Testing Laravel Apps With Confidence
A test suite is only worth its maintenance cost if it catches real bugs and rarely lies. Most slow, brittle suites got that way by testing the wrong things. Here is how to build one that earns its keep.
The Testing Pyramid, Pragmatically
The classic advice is many unit tests, fewer integration tests, few end-to-end tests. In a Laravel app the useful reframing is: test behaviour through the framework, not implementation details.
Feature Tests First
Feature tests hit a route, exercise real validation, database, and responses, and assert on outcomes. In Laravel they are fast enough to be your workhorse and realistic enough to catch actual regressions.
Unit Tests for Logic
Reserve pure unit tests for genuine business logic: calculators, state machines, parsers. Anything with branching rules deserves isolated tests; a thin controller does not.
What Not to Test
Coverage is a means, not a goal. Chasing 100 percent tests framework code you do not own.
Skip the Framework
Do not test that Eloquent saves a row or that a route resolves. You are testing Laravel, not your app. Test the behaviour you built on top.
Keeping Tests Fast
A slow suite is a suite people skip.
Database Strategy
Use an in-memory SQLite database or transactions that roll back per test. Refresh only what you must, and factory only the data each test truly needs.
Avoid Sleep and Real Time
Freeze time with `travelTo()` instead of sleeping, and fake external services. A test that waits on the clock or the network is a test that flakes.
Closing Thoughts
Aim for a suite that is mostly feature tests, targeted unit tests for real logic, fast by construction, and honest about what it covers. That suite gives you the confidence to refactor, which is the entire point.