I decided to switch from SQLite to MySQL for Laravel testing. You can read about the reasons here.
I always use Docker and VSCode’s devcontainer for my development environment. Therefore, I needed to update my Docker Compose file and add a database service specifically for when running pest.
For running pest, I created a separate service and run it with docker compose run --rm -it pest.
I also grouped it with cli as a profile so that it does not run automatically when bringing up the entire container using docker compose up -d.
Now, let’s add the database service for testing only.
We need to ensure that pest only runs after mariadb-testing is up, which is why we added the depends rule for the pest service.
Before running it, let’s also adjust phpunit.xml.
Now, let’s run it, and… we encounter an error.
Even though the database service is already running, it’s not ready for the connection. However, after a few retries, the test passes.
Therefore, the healthcheck in mariadb-testing is not effective, as it gives a false positive result.
The solution is to add another service using the busybox image, which essentially waits for the database to be fully ready for connection.
Now, when running the pest service, the testing will wait until the database is fully ready 🎉.
Tips
I’ve created some bash aliases to help me run docker compose and docker compose run.
Now, I just need to run these from my project folder.