The Steps I Took To Get Laravel Testing Database and Testing Environment Setup

TDD, test driven development

At first I was trying to set up the testing env using a tutorial from laravel 5.3 which was a pain because I ended up wiping out my local db. I finally figured out that I was doing it wrong sense I am using Laravel Framework 5.8.36.

I wiped out the local db because I was using the .env file not the new .env.testing file and was running $this->artisan('migrate:reset'); in the tearDown method. Which, at the time of writing this, I don’t think I need. I believe I can use the DatabaseMigrations trait. Will update the code once I learn more about that. Anyway, just glad I didn’t have the production env creds in the .env file … that would have been a bad day!!!

First I added a new connection to the connections array in the database.php config file:

'testing' => [
            'driver' => 'mysql',
            'host' => env('DB_TEST_HOST', '127.0.0.1'),
            'port' => env('DB_TEST_PORT', '3306'),
            'database' => 'testing_db_name',
            'username' => env('DB_TEST_USERNAME', 'somedefaultusername'),
            'password' => env('DB_TEST_PASSWORD', 'somedefaultpassword'),
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
            'strict'    => false,
        ],

Then I created the .env.testing file in the same directory as the .env file and put the following code inside:

DB_TEST_CONNECTION=testing
DB_TEST_HOST=db
DB_TEST_PORT=3306
DB_TEST_USERNAME=somedefaultusername
DB_TEST_PASSWORD=somedefaultpassword

Following that I opened sequel pro that i have installed on my machine and created the testing_db_name database (look up the sequel pro docs if you don’t know how to do this, it’s really easy). Of course I added my user to the db and granted the permissions needed to run my tests. You can add the user by running this query:

GRANT ALL PRIVILEGES ON testing_db_name.* TO 'somedefaultusername'@'localhost' IDENTIFIED BY 'somedefaultpassword';

Here’s a good article explaining more sql commands you can run from the command line or in the query tab within sequel pro.

Now, that we have the database connection and have actually created it, it’s time to clear the laravel config cache so we can use the testing env. We do this by running this command from the command line: php artisan config:cache --env=testing This changes the config.php file located at /bootstrap/cache/config.php

Finish up by migrating your testing database with php artisan migrate --database=testing and then run your tests running vendor/bin/phpunit on the command line.

Thanks for reading. I hope this was useful and if you have a second please share it on social media.

One comment

  1. […] Of course, there are some flaws, mostly human/developer flaws, when it comes to TDD. One being that it can take a long time to run all the tests when you have an expansive set tests. Another is that you may not have your data store or environment set up correctly and you wipe out your data store. See my post about how I got the Laravel Testing Database and Testing Environment Setup. […]

    Like

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.