dees040's avatar

Speed up unit tests

A few months ago I created this package to speed up my Laravel tests with almost 100%. It was inspired by this article.

Now I was wondering, do you might know a way to speed up the tests even more? Feedback on the package in general is always welcome.

0 likes
3 replies
Tray2's avatar

I use an in memory sqlite database when doing my testing.

PHPUnit 7.3.4 by Sebastian Bergmann and contributors.

................................................................. 65 / 85 ( 76%)
....................                                              85 / 85 (100%)

Time: 5.24 seconds, Memory: 26.00MB

OK (85 tests, 283 assertions)
PHPUnit 7.3.4 by Sebastian Bergmann and contributors.

................................................................. 65 / 85 ( 76%)
....................                                              85 / 85 (100%)

Time: 5.27 seconds, Memory: 26.00MB

OK (85 tests, 283 assertions)

Add these two lines to your phpunit.xml

        <env name="DB_CONNECTION" value="sqlite"/>
        <env name="DB_DATABASE" value=":memory:"/>
dees040's avatar

The package does this for you. It also saves content of the database in a static string after the first migrations. Before each test it puts the string in the sqlite memory, which makes it even more quicker than just using the :memory: database.

Tray2's avatar

Since I run single test 99.9% of the time and only run the whole suite once in a while I think 5.3 seconds is fast enough 85 tests and 283 assertions. :)

Please or to participate in this conversation.