Jose Gabrielle Rivera
jgrivera.dev

Follow

jgrivera.dev

Follow
Using SQLite to dramatically increase speed of integrated tests in Lumen

Photo by Uriel SC on Unsplash

Using SQLite to dramatically increase speed of integrated tests in Lumen

Jose Gabrielle Rivera's photo
Jose Gabrielle Rivera
·Aug 3, 2021·

2 min read

Table of contents

  • Problem
  • Solution
  • Results
  • Closing thoughts and risky caveats

Problem

I had a problem in Lumen where each of my integrated tests ran for about a minute. This was slow and counterproductive for me. My initial setup was using a separate MySQL testing database to conduct the integrated tests. I tried to use an SQLite connection instead and I surprisingly garnered some surprising results.

Solution

In our case, override the testing environment variables in your phpunit.xml to use SQLite instead.

<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>

This will tell the testing environment to set up an in-memory database.

Results

As an example, my test suite uses the Lumen DatabaseMigrations trait and has three feature test cases.

Using MySQL:

PHPUnit 9.5.6 by Sebastian Bergmann and contributors.

...                                                                 3 / 3 (100%)

Time: 03:21.588, Memory: 28.00 MB

Using SQLite:

PHPUnit 9.5.6 by Sebastian Bergmann and contributors.

...                                                                 3 / 3 (100%)

Time: 00:00.475, Memory: 30.00 MB

It is a dramatic reduction from 3 minutes to 475 milliseconds. How surprising!

Closing thoughts and risky caveats

Although blazingly fast, proceed to use SQLite with caution! There may be some drawbacks in using a database engine that is different from your production environment.

We can never guarantee that the behavior of using an SQLite database would reflect the same behavior in a MySQL behavior. At best, use the same database engine for both your testing and production database.

Using a more conservative RefreshDatabase trait with a separate same-engine testing database is commonly advised. However, in Lumen, the aforementioned trait does not exist and only exists in Laravel.

 
Share this