I've been running my tests in an on disk mySQL database since I started, I'll tell you how I did it.
First, you create an alternate database called something like appName_test.
Then, you want to go to your config/database and add a test connection to the list, like this:
// Note the env() variables, they are specific to the test database.
'mysql_test' => [
'driver' => 'mysql',
'host' => env('DB_TEST_HOST', '127.0.0.1'),
'port' => env('DB_TEST_PORT', '3306'),
'database' => env('DB_TEST_DATABASE', 'forge'),
'username' => env('DB_TEST_USERNAME', 'forge'),
'password' => env('DB_TEST_PASSWORD', ''),
'unix_socket' => env('DB_TEST_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
Of course, you have to define those env variables in your .env file.
Then you need to setup your phpunit.xml to use the testing database during testing, like this:
<php>
<env name="APP_ENV" value="testing"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="QUEUE_DRIVER" value="sync"/>
<!-- Add this line, this is the connection name we defined before. -->
<env name="DB_CONNECTION" value="mysql_test"/>
</php>
Your testing database is empty though, run migrate on it:
php artisan migrate --database mysql_test
You can also run seed on it, if your tests need to have some data in the database. Mine inserted all the data during fixture setup.
Now, if your TestCases are using the DatabaseTransactions trait. Each test will run inside a different transaction that will be automatically rolled back at the end of the test, undoing any change to the database made during the test.
Because of that, I only need to run migrate again when I create new migrations.
Dont forget to clear your config cache before running phpunit!
Good luck, tell me how it goes.