Skip to the main content
{}
Back to all articles

Technical article

Pest tests for Laravel: a risk-driven testing strategy

Test critical routes, forms, authorisation, idempotency and deployment contracts instead of pursuing line coverage alone.

By Codegenie2 minute read
  • Laravel & PHP
  • Pest
  • tests
  • Laravel
  • risk-driven
  • testing
Code window, PHP versions and software management dashboard.
In this article

Start Laravel tests with failures that have concrete impact: unauthorised access, lost inputs, duplicate processing or success after a failed operation. Pest provides readable tests; their quality depends on scenarios and assertions rather than the highest possible count.

Turn risks into testable outcomes

  • Authorisation: a user can read and change only permitted records.
  • Validation: invalid inputs cause no mail, payment or database change.
  • Repetition: retrying does not create an unintended duplicate.
  • Dependency failure: a provider error produces an honest response and allows recovery.

Illustration: an enquiry with fake mail

Test the risky user flow, not just the helpertests/Feature/ContactFormTest.phpphp
it('accepts a valid contact request', function (): void {
    Mail::fake();

    $response = $this->post(route('contact.store'), [
        'name' => 'Example User',
        'email' => 'user@example.test',
        'message' => 'I would like to discuss a project.',
    ]);

    $response->assertRedirect();
    Mail::assertSent(PublicFormMail::class);
});

The fragment assumes an existing contact.store route and PublicFormMail in a Laravel application. Mail::fake prevents real mail transport. It checks the redirect and sent mailable; add exact destination, recipient and relevant validated data assertions according to the application contract.

Also test that nothing happens

Create a separate invalid-email scenario and assert the validation error plus Mail::assertNothingSent(). A queued flow needs assertNothingQueued and its queue contract. A fake proves an application decision, not a real provider connection or inbox receipt.

Select the test level for the failure

A unit test suits a pure calculation. An HTTP feature test protects middleware, validation, authorisation and response together. A browser test helps with focus, step changes or client behaviour that an HTTP test never executes. Limit browser coverage to meaningful user risks.

Existing projects and CI

Check locked Pest, Laravel and PHP versions before copying installation commands; use their matching documentation. Run relevant tests in CI and stop on regressions. A passing local fragment is not a full project test, while coverage alone cannot show that the important failure path is covered.

Code examples are illustrative. They have not been executed in a complete Laravel project; adapt imports, relationships, routes and configuration to your project and test actual behaviour.

Technical deep dive: test the risk, not only the happy path.

Illustrative scenario: an endpoint works for the owner of a record. The relevant regression is not only a 200 response, but also that another authenticated user cannot perform the same mutation.

Technical decision

Start from the business risk and define a scenario around it. For authorisation, that means at least the allowed actor, the denied actor and the state of the data after both requests.

Checks

  • Check the HTTP status and verify that forbidden data was not changed.
  • Use a fixture where ownership is explicitly different.
  • Test a valid mutation separately from validation and authorisation failures.
  • Keep the test focused on public behaviour so refactors cause less unnecessary test breakage.

The example is illustrative. The right tests follow from the risks and contracts of the concrete application.

Last updated:

Need technical context for your own project?

Codegenie helps with Laravel applications, existing codebases, shared-hosting deployments and technical improvements.