Testing Laravel applications with in-memory SQLite
Published 2026-11-06
The safety argument
The first rule of database testing is that a test must never be able to reach real data. Pointing tests at a shared development database invites accidents: a wipe step in a test setup running against the wrong connection is a classic way to lose a day. An in-memory SQLite database removes the risk by construction, since it exists only for the duration of the test process.
How it works
Configure the testing environment to use the sqlite driver with the special in-memory database name. Each test run builds the schema from your migrations, and a refresh trait resets state between tests. Because nothing is written to disk, the suite is fast, which encourages running it often.
Keep the configuration in the test environment file so it is impossible to start the suite with production credentials by mistake, and avoid running the suite inside application containers that hold real connection settings.
Know the limits
SQLite is not MySQL or PostgreSQL. It is more permissive about types, handles foreign keys and some alter statements differently, and lacks engine-specific functions such as JSON operators or full-text search with the same behaviour. Code that relies on those features can pass on SQLite and fail in production.
Migrations that use raw SQL for a specific engine need a guard so they skip on SQLite, and your test setup should say so clearly.
Cover the gap
- Keep a small set of integration tests that run against the real engine in a disposable container, in CI.
- Prefer the query builder and Eloquent over raw SQL, so behaviour is portable.
- Test migrations both up and down on the engine you deploy to.
- Use factories to build realistic data instead of fixtures copied from production.
What to test first
Start with the logic where mistakes cost money or trust: permissions, billing state changes, data deletion and anything involving encryption keys. A few well-chosen tests around those paths protect more than hundreds of tests of trivial getters.
A first test, step by step
Pick a behaviour that matters, such as a user without the right role being refused access to an admin route. Create the user with a factory, act as that user, call the route and assert on the response status. The test needs no seeded data beyond what it creates, runs in milliseconds and leaves nothing behind, which is exactly what makes it worth writing.
Keeping the suite healthy
- Name tests after the behaviour, not the method.
- Build data with factories so each test states only what it needs.
- Fake external services such as mail, queues and HTTP calls, so tests never reach the network.
- Run the suite in continuous integration on every push, and fix a red build before anything else.
- Delete tests that nobody trusts; a flaky test teaches people to ignore failures.
Where tests pay back
Tests are most valuable around the code you are afraid to change. Before upgrading a framework version, a permission check or a billing rule, a small set of focused tests turns a risky edit into a routine one and tells you at once what the change affected.