DEV Community

Michael Heap
Michael Heap

Posted on • Originally published at michaelheap.com on

Dedicated test databases with Laravel Sail

I’ve been playing around with Laravel Sail on a project that I’ve just started and wanted to be able to run my tests on a separate database and leave my main DB intact for development.

Adding a second database for tests involves adding a second container and configuring your tests to point at the newly created host.

Add the following to docker-compose.yml under the services: key:

mysql_test:
  image: "mysql:8.0"
  environment:
    MYSQL_ROOT_PASSWORD: "${DB_PASSWORD}"
    MYSQL_DATABASE: "${DB_DATABASE}"
    MYSQL_USER: "${DB_USERNAME}"
    MYSQL_PASSWORD: "${DB_PASSWORD}"
    MYSQL_ALLOW_EMPTY_PASSWORD: "yes"
  networks:
    - sail
Enter fullscreen mode Exit fullscreen mode

Next, configure your .env.testing file to point at the mysql_test host (Docker Compose uses the service name as the host name for networking):

# Don't forget to set APP_KEY too
DB_HOST=mysql_test # This is the important bit
DB_DATABASE=
DB_USERNAME=root
Enter fullscreen mode Exit fullscreen mode

Then finally, run your tests:

./vendor/bin/sail test
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

Playwright CLI Flags Tutorial

5 Playwright CLI Flags That Will Transform Your Testing Workflow

  • --last-failed: Zero in on just the tests that failed in your previous run
  • --only-changed: Test only the spec files you've modified in git
  • --repeat-each: Run tests multiple times to catch flaky behavior before it reaches production
  • --forbid-only: Prevent accidental test.only commits from breaking your CI pipeline
  • --ui --headed --workers 1: Debug visually with browser windows and sequential test execution

Learn how these powerful command-line options can save you time, strengthen your test suite, and streamline your Playwright testing experience. Practical examples included!

Watch Video 📹️