Testing with SQLite in memory: when it lies to you
An in-memory SQLite database makes a Laravel test suite run in seconds. Here are five bugs it let through to production for us, and how we catch them now.
SQLite is a fantastic database. It is just not the database you run in production, and the differences show up in exactly the places where bugs hurt most.
Five differences that matter
- Types: SQLite stores
'42abc'in an integer column without complaining. - String comparison: MySQL compares case-insensitively by default, SQLite does not.
- Foreign keys: they are off unless you enable them.
- Locking:
lockForUpdate()does nothing useful. - JSON: the JSON functions and their results differ in edge cases.
Run both
We keep SQLite for the fast local loop and run the full suite against MySQL in CI. A tiny trait marks the tests that only make sense on the real database.
php
trait RequiresMysql
{
protected function setUpRequiresMysql(): void
{
if (DB::getDriverName() !== 'mysql') {
$this->markTestSkipped('Needs MySQL locking semantics');
}
}
}