Testing use a different configuration
Posted: 2025/04/25
When practicing Testing today, I got this weird error after running php artisan test:
SQLSTATE[HY000]: General error: 1 no such table: users (Connection: sqlite,
I already changed DB_CONNECTION to mysql, why sqlite error showed up here?
It turns out when running artisan test, Laravel uses a separate configuration file for testing, which is located in the phpunit.xml file. I need to edit that file to change the DB_CONNECTION value to mysql, like this
<env name="DB_CONNECTION" value="mysql"/>
Alternatively, I can also use the --env option when running php artisan test
php artisan test --env=mysql
A good lesson learnt today!