laravel / ideas

Issues board used for Laravel internals discussions.
938 stars 28 forks source link

Running tests in a separate database with Laravel Sail #2478

Open djam90 opened 3 years ago

djam90 commented 3 years ago

It is often desirable to run tests (either unit tests or Dusk tests) in a separate database from your development one. This isn't too hard if you manually set up your development environment, many people add an SQLite database or another MySQL database and point the testing environment at the new database.

The docker-compose.yml file included with Laravel Dusk includes either a mysql or postgres service to run a development database.

My original proposal and pull request was to simply add a copy of the mysql service for tests, but this was rejected and I understand why. After discussion on Discord I have thought of some other proposals:

Proposal 1 - Add a second database to the mysql and postgres service This would involve including a shell script which would create a second testing database in the mysql/postgres services. Then you can update your testing .env file to point to the testing database.

Proposal 2 - Run a docker container on the fly when running tests with a command such as sail dusk. This would involve amending the sail script to first run a docker database service with docker run, and then amend the .env config to point to this new database host.

I think it is important that tests are run in a different database to the local development database, this is how it works in other language frameworks such as Ruby on Rails.

If anyone has any other ideas to solve this problem I would be happy to write the pull request.