Covers Laravel 13 testing with Pest 5 / PHPUnit 13 (Pest 4 / PHPUnit 12 on PHP 8.3): feature tests (HTTP,
full stack), unit tests (isolated classes), and architecture tests; Pest
syntax (it/test/describe), datasets; HTTP testing (requests, JSON
assertions, auth/actingAs, status/redirect assertions); database testing
(RefreshDatabase, factories, DB assertions); mocking (services, spies,
Mail/Queue/Event fakes, Http::fake, time travel); console/Artisan command
tests; and PHPUnit-attribute-based seeding (#[Seed], #[Seeder]).
Laravel Testing
Agent Workflow (MANDATORY)
Before ANY implementation, spawn 3 agents in parallel, one Agent call each with a name:
- fuse-ai-pilot:explore-codebase - Analyze existing test patterns
- fuse-ai-pilot:research-expert - Verify Pest/PHPUnit docs via Context7
- mcp__context7__query-docs - Check assertion and mocking patterns
After implementation, run fuse-ai-pilot:sniper for validation.
Overview
| Type | Purpose | Location |
|---|
| Feature | HTTP, full stack | tests/Feature/ |
| Unit | Isolated classes | tests/Unit/ |
| Arch | Code architecture | tests/Arch.php |
Decision Guide: Test Type
Decision Guide: Test Strategy
Critical Rules
- Use RefreshDatabase for database isolation
- Use factories for test data (never raw inserts)
- Mock external services - Never call real APIs
- Test edge cases - Empty, null, boundaries
- Run parallel -
pest --parallel for speed
Reference Guide
Pest Basics
HTTP Testing
Database Testing
Mocking
Other
Templates
Quick Reference
Commands
Best Practices
DO
- Use
RefreshDatabase trait
- Follow AAA pattern (Arrange-Act-Assert)
- Name tests descriptively
- Test one thing per test
- Use factories for data
DON'T
- Create test dependencies
- Call real external APIs
- Use production database
- Skip edge cases
Laravel 13 Notes
PHPUnit 12/13 + Pest 4/5
The Laravel 13 skeleton ships PHPUnit 12 (^12.5); the framework also accepts PHPUnit 13. Pest 5 (current, requires PHP 8.4+ and PHPUnit 13, plus pestphp/pest-plugin-laravel ^5.0 which needs Laravel ^13.23) — stay on Pest 4 (PHPUnit 12) if the app still runs PHP 8.3. PHP attributes replace docblock annotations.
Str factories reset
Laravel 13 resets custom Str factories (UUID / ULID / random string) during test teardown. Tests relying on a factory persisting across methods must set it in each test or setup hook.
#[UnitTest] (L13)
Illuminate\Foundation\Testing\Attributes\UnitTest on a test method skips booting the application for that test.
Migration from Pest 3 → 4
- Bump
pestphp/pest and all Pest plugins to ^4.0 (PHP 8.3+, PHPUnit 12)
- Snapshot names changed: run
./vendor/bin/pest --update-snapshots
pest-plugin-watch and pest-plugin-faker are archived
Migration from Pest 4 → 5
- Requires PHP 8.4+; bump
pestphp/pest and all Pest plugins to ^5.0 (built on PHPUnit 13)
- New in 5:
--tia (test impact analysis, local only), first-party PHPStan/Rector plugins, Agent and Evals plugins, time-balanced sharding