It looks like you've set up most of the configuration correctly for using SQLite as your testing database. However, there's a small discrepancy in your .env.testing file that might be causing the issue. You've set DB_DEFAULT instead of DB_CONNECTION. The DB_CONNECTION environment variable is what Laravel uses to determine which database connection to use.
Here's how you can fix it:
- Update your
.env.testingfile to use the correct environment variable for specifying the database connection. ChangeDB_DEFAULTtoDB_CONNECTION:
DB_CONNECTION=sqlite
DB_DATABASE=:memory:
-
Make sure that your
.env.testingfile is being used when running Pest tests. You can specify the environment file to use by setting the--envoption when running Pest, or by configuring your testing environment to automatically pick up the.env.testingfile. -
Ensure that your PHPUnit configuration in
phpunit.xmlis not being overridden somewhere else. Pest uses this file, so the settings here should be respected. -
If you're using Laravel, you can also set the configuration programmatically in your test setup method to ensure that the SQLite database is being used:
public function setUp(): void
{
parent::setUp();
config()->set('database.default', 'sqlite');
config()->set('database.connections.sqlite.database', ':memory:');
}
- Finally, make sure to clear your configuration cache before running tests, as cached configuration might still be pointing to the MySQL database. You can do this by running:
php artisan config:clear
Or, if you're using Pest, you can add a script in your composer.json to clear the config before running tests:
"scripts": {
"test": [
"php artisan config:clear",
"pest"
]
}
Then you can run your tests with composer test.
After making these changes, your tests should run using the SQLite database in memory, and your local MySQL database should remain untouched.