Custom Laravel Testing Conventions
Scope
Apply this skill for test-related tasks in Laravel projects.
Core Testing Workflow
Follow t_wada TDD:
- •Write a test list.
- •Pick one case and write a failing test.
- •Implement the minimum code to pass all tests.
- •Refactor.
- •Repeat until test list is empty.
Pest Rules
- •Write tests in Pest style.
- •Import Pest Laravel functions when used:
- •
use function Pest\Laravel\actingAs; - •
use function Pest\Laravel\mock;
- •
- •
describe('<method-name>')must match the subject public method.
Command
Run tests using:
bash
./vendor/bin/sail test [filepath]
beforeEach Pattern
- •Create local variables first, then assign to
$thisproperties. - •Exception: static literal values that do not come from factories.
- •Set fakes in
beforeEachwhen needed (Event::fake([...]),Storage::fake(...)).
Example:
php
beforeEach(function () {
$user = User::factory()->createOne();
actingAs($user);
$this->user = $user;
});
Dataset Pattern
- •Use
->with()when cases can be combined. - •Keep multiline function arguments for dataset-driven tests.
- •For multiple parameters in dataset rows, use named variables in values for readability.
Factory Pattern
- •Prefer factory state methods when available.
- •Use
->forEachSequence()when all patterns must be covered. - •Use
->createOne()/->createMany()for better return types. - •Prefer
::factory(x)over->count(x).
AAA Comments
Use AAA comments:
php
// Arrange // Act // Assert
Use // Act & Assert for compact tests only.
Feature Test Pattern
- •Use
route('...')to build request URLs. - •Prefer combining request and assertion fluently when clear.
Example:
php
$response = post(route(...)) ->assertValid() ->assertRedirect();