Unit testing models in Laravel 5 with SQLite

So you want to run Unit tests with PHPUnit on your Laravel 5 application, and do some actual database interaction instead of mocking data?

I do that for one of my applications and I’m using SQLite with in memory database to increase performance event more. I’ll quickly show how i have set it up.

Config and environment in Laravel 5

Laravel 5 uses the DotEnv library for environment configuration. That means that you have .env file in the project root, to define variables specific to your enviroment, and also one .env in the production to specify the environment.

Your local .env file could look something like this:



It’s just key-value pairs. They are used by the different config files.

The config/database.php file (rows not related to this text is removed):

return [
    'default' => env('DB_DEFAULT_CONNECTION', 'mysql'),
    'connections' => [
        'testing' => [
            'driver'   => 'sqlite',
            'database' => ':memory:',
            'prefix'   => '',
        'mysql' => [
            'driver'    => 'mysql',
            'host'      => env('DB_HOST', 'localhost'),
            'database'  => env('DB_DATABASE'),
            'username'  => env('DB_USERNAME'),
            'password'  => env('DB_PASSWORD'),
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
            'strict'    => false,

As you see, the config/database.php has references to the variables in the .env file using the env() function, with looks like env(key, default). that functions get the corresponding value from the .env file. And one that is particular interesting in this post is DB_DEFAULT_CONNECTION. As you saw in the .env file that was set to mysql. Laravel will be using the mysql connection with the host, database and credentials configured in the .env file.

Update As noted in the comment, From Laravel 5.1 the database connection is named DB_CONNECTION in the environment variable.

Testing environment

But when we run phpunit we want to use the testing connection from the config/database.php with uses in-memory sqlite database.

This is done by setting php environment variables in the phpunit.xml file:

<?xml version="1.0" encoding="UTF-8"?>
        <env name="APP_ENV" value="testing"/>
        <env name="DB_DEFAULT_CONNECTION" value="testing"/>

This will set the environment to testing with APP_ENV (if you use that in config/app.php) and also select the testing connection in the database config when you run phpunit.

Update Note here, that the env name in your phpunit.xml should be the same as used in the database.php config file


2 Replies to “Unit testing models in Laravel 5 with SQLite”

  1. In my version of Laravel 5.1 doesn’t work as you put it there. I had to replace DB_DEFAULT_CONNECTION with DB_CONNECTION.

    Looking at my config/database.php Laravel has

    return [
    'default' => env('DB_CONNECTION', 'mysql'),

    So could have been updated after your post, which has been of great help btw. Thanks a lot, now my tests run well.

  2. Thanks for the notice and replay. I have updated the post now with the new name for the connection and added a notice to check that the same name is used in both database config and phpunit.xml

Leave a Reply

Your email address will not be published. Required fields are marked *