I had a problem in Lumen where each of my integrated test run for about a minute. This was really 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.
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.
As an example, my test suite uses the Lumen
DatabaseMigrations trait and has three feature test cases.
PHPUnit 9.5.6 by Sebastian Bergmann and contributors. ... 3 / 3 (100%) Time: 03:21.588, Memory: 28.00 MB
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 with your production environment.
We can never guarantee that the behavior with using a 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.