Guides · SaaS
How to write Laravel tests that matter: what to test and what to skip.
A practical approach to Laravel testing with Pest or PHPUnit: feature tests, factories, policies, money, webhooks and tenant isolation.
Key takeaways
- Test behaviour that would hurt if it broke: money, permissions, tenant isolation, and core flows.
- Feature tests that hit routes and the database give the best value for Laravel apps.
- Use factories and an in-memory or test database so tests are quick and repeatable.
- A test count is not a quality measure. A few meaningful tests beat many shallow ones.
Pest or PHPUnit
Laravel supports both. PHPUnit is the underlying engine. Pest is a friendlier syntax built on top of it. Pick one for readability and stay consistent. Both can run in the same project.
Start with feature tests
A feature test makes a request and checks the result, including the database:
it('creates a booking for a logged-in customer', function () {
$user = User::factory()->create();
$this->actingAs($user)
->postJson('/api/bookings', ['date' => now()->addDay()->toDateString()])
->assertCreated();
$this->assertDatabaseHas('bookings', ['user_id' => $user->id]);
});
Use the RefreshDatabase trait so each test starts clean.
What to test first
- Money paths. Totals, discounts, tax, refunds, partial payments.
- Permissions. A normal user must get 403 on admin routes.
- Ownership. User A cannot read user B's record by changing an ID.
- Tenant isolation. Create two tenants, then assert one cannot see the other's rows.
- Webhooks. A request with a bad signature is rejected, and a good one is processed once.
- Core flows. Sign up, log in, the main thing your product does.
Tenant isolation example
it('does not show another tenant\'s patients', function () {
$a = Tenant::factory()->create();
$b = Tenant::factory()->create();
Patient::factory()->for($b)->create(['name' => 'Hidden']);
$this->actingAs(User::factory()->for($a)->create())
->getJson('/api/patients')
->assertOk()
->assertJsonMissing(['name' => 'Hidden']);
});
This one test can catch the most damaging class of bug in a SaaS. See multi-tenancy explained.
Factories and fakes
- Define factories for each model so tests stay short.
- Use
Mail::fake(),Queue::fake(),Http::fake()andEvent::fake()to avoid real side effects and assert that they were triggered. - Never call real payment or messaging services in tests.
Unit tests
Use unit tests for pure logic, such as a price calculator, with no database. They are the fastest and best for rules with many edge cases.
Keep tests fast and reliable
- Use SQLite in memory or a dedicated test database.
- Avoid sleeping or depending on real time. Use
Carbon::setTestNow(). - Do not share state between tests.
- Run tests on every push in CI.
What not to test
- The framework itself, such as whether
Model::createworks. - Simple getters with no logic.
- Exact HTML where a contract will change often.
Frequently asked questions
How many tests are enough?
There is no number. Ask whether the riskiest parts of your system would fail loudly if broken.
Should I do TDD?
It works well for pure business rules. For UI-heavy work, writing tests right after the feature is often enough.
Do tests slow development?
They cost time at first and save it when you change code with confidence.
Tests are how I keep multi-tenant SaaS safe to change.
See the credentialing SaaS