I am following along with "Pest from Scratch" and when I run php artisan test my database is cleared almost entirely. I think I need to specify an alternative test database but it is unclear to me how I would do that yet. I appreciate any and all help.
It sounds like your tests are running against your main database instead of a separate test database. This is a common issue but can be resolved by configuring Laravel to use a different database when running tests.
Here’s how you can set up a separate test database:
Create a Test Database: First, create a new database that will be used exclusively for testing. You can do this through your database management tool (like phpMyAdmin, MySQL Workbench, etc.).
Configure Environment: Open your .env.testing file in the root of your Laravel project. If it doesn't exist, create it by copying your .env file and renaming it to .env.testing. In this file, change the database configuration to point to your new test database. For example:
Make sure to replace laravel_test, root, and the password with the appropriate values for your test database.
Configure PHPUnit: Open your phpunit.xml file in the root of your Laravel project. Ensure that the environment variables for the database connection are set to use your test environment settings. It should look something like this:
Run Migrations for Test Database: Before running your tests, make sure your test database has all the necessary tables. You can run migrations specifically for your test database by using:
php artisan migrate --env=testing
Run Tests: Now, when you run php artisan test, it should use your test database instead of your main database.
By following these steps, you can ensure that your main database remains unaffected when running tests, as all testing operations will be performed on the separate test database.
@bryanhoffman Why tables are deleted is because in the Pest.php, there are trait RefreshDatabase, which deletes all tables from the database. You need to remove this trait if you do not want to delete tables from DB. Or like Larry suggested, create a new .env.testing file
This does not work with Laravel 11. I also tried setting the environment variables in phpunit.xml, but that did not work either.
Removing the RefreshDatabase trait could be an option, but I'm using it in a lot of tests and also for custom DB commands, so I'd rather keep it.
For the time being, I'm stuck on Laravel 10, although the actual application seems to work just fine. But I cannot ship it without green tests ¯\(ツ)/¯
2 years later and this is still a problem. The only way I am able to prevent it is by running php artisan config:clear before php artisan test. If that first command is forgotten, the database is gone.
If that first command is forgotten, the database is gone.
Which database? Testing, dev or production? Purging testing database is normal behavior in standard Laravel workflow, it can be tuned to your wish of course.
Happened to me once as I forgot to switch to a test server and cached my dev env instead, suddenly my DB was gone :P
You can add this check in your Pest.php to avoid running tests if configs are cached for some reason:
if (file_exists(__DIR__.'/../bootstrap/cache/config.php')) {
throw new \Exception('Refusing to run tests - not using in-memory sqlite. Check for cached config.');
}
@kylef-fotf you can also create your own test Artisan command which executes config:clear beforehand. Or tune composer run test command to do so (see composer.json / scripts section). This way you never forget to clear cache before tests.
But why your config is cached in dev environment in the first place? It shouldn't be normally.