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

Crinsane's avatar

Use sqlite in memory db with codeception

Okay, I feel like a total dumb-ass right now, because it seems like I'm totally missing the obvious... I am trying out Codeception, using some of the vids on here. And just as with my other tests, I'd like to use an in memory database.

I've set up the stuff in /testing/database.php, it's working in other tools like plain old PHPUnit. But with Codeception I keep getting errors about table not found etc.

So my question; Is it possible to use an in memory database with Codeception, and how do I migrate (and optionally seed) it for every test?

0 likes
9 replies
chrio's avatar

From what I have read it should be possible, but it might be faster to have a prepared sqlite database copied before the tests instead.

Take a look at this blogpost by Chris Duell for more info.

1 like
chrio's avatar

because after all, you want the database to be empty/clean on every test. Don't really think having to 'artisan migrate' the database manually every time.

That is not what the later part of that post suggest you do.

In that he will set up the sqlite database like he wants it, with a migration and seeded data (if neccesary). The resulting database is then saved to a separate file, let's call it testdb_clean.sqlite.

Before the tests you then copy testdb_clean.sqlite to your testing database testdb.sqlite and run your tests on that. If you are using gulp or something similar you can have it done automatically before each test.

I am refering to this part of his post:

After getting fed up enough to find a faster way to setup and destroy the database for each and every test that needed it, I started to look at alternate database types. And one stood out, big time. sqlite being a flat file meant that I could simply have a prebuilt and seeded database I could just copy that file and run a test on it then trash the file. Then just rinse and repeat for each test I needed a database for.

Hope this is of help to you.

Crinsane's avatar

Hmm... might not have read it as well as I thought I did ;) Let me reread ;)

bagwaa's avatar

I also read this post recently, I think the biggest issue is when you have quite a big MySQL database that you need to convert to SQLite, I can't seem to find a clean way todo this.

Crinsane's avatar

@bagwaa But why would you want to convert the entire database? Wouldn't you just want the schema (which I guess you have as migrations) and just a bit of seeding? Not a copy of the actual database...

bagwaa's avatar

yeah, sorry I wasn't being clear, I only want to convert the structure with the migrations (no seeding). The problem is when you have a migration than ALTERS a table, it tries to use a null value on fields that are not listed strictly as nullable()

See this thread..

http://forumsarchive.laravel.io/viewtopic.php?id=13320

MikeHopley's avatar

I just tried Chris Duell's "sqlite dump" approach, having previously been using sqlite in-memory.

It blows the in-memory database out of the water. 15 times faster is no exaggeration. And to be clear, the database is reset each test.

The only downside is that you have to keep your sqlite dump up to date. Chris discusses ways to do this in his article. I do it by using a custom command when migrating the DB, so all my DB stuff gets refreshed in one command.

This is for functional/integration tests, in my case. For Codeception acceptance tests, I'm still using a mysql dump to repopulate the database. I hadn't really thought about switching this to sqlite. Hmm...

Please or to participate in this conversation.