Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

drewdan's avatar
Level 15

SQLite in Memory for PHPUnit Testing

Hi All,

I am working on optimising our unit tests, we are currently using a MySQL database to run our tests, and it takes quite a great deal of time and we only have currently about 20% code coverage.

Our plan was to use an SQLite database held in memory to speed up the performance. However, with every test, I need to migrate and seed the data. The seeding is slowing the process down considerably.

Can I migrate and seed the data once before the tests start, and then run all of my tests, or is it pretty standard to do the testing in this manner? I appreciate every test in isolation with the same start data is the best way to do it, but we are looking to balance our performance too.

Any thoughts and tips would be great.

Thanks

0 likes
2 replies
bobbybouwmann's avatar
Level 88

Yeah you can use sqlite as well when first migrating and seeding the database. So you have two options here:

  1. Use sqlite in memory, this means you have to migrate the database every time and seed the database for every tests. It depends a little bit on how you write your tests, but this can win you a lot of time.

  2. Use an sqlite file. Instead of having a mysql database you use a sqlite file. In this case you also need to migrate and seed the database before you run the tests

Normally I use the RefreshDatabase trait in all of my tests. This way you can be sure that the database is migrated and empty. You can then seed the database in your tests using factories. This way you don't have to much data when running each test. If you look closely in the code you can see that it does a migrate:fresh once and after that it starts a transaction for each test. This way the tests are much faster!

Source: https://github.com/laravel/framework/blob/master/src/Illuminate/Foundation/Testing/RefreshDatabase.php

1 like
drewdan's avatar
Level 15

I like the in memory idea, so I think I will stick with this approach. Just need to work out why all my tests keep failing authorisation now!

Cheers for the information :D

Please or to participate in this conversation.