// Comprehensive guidance for writing Pest v4 tests in Laravel applications, including feature tests, unit tests, and browser tests. Use this skill when writing tests, implementing test-driven development, testing APIs, creating browser automation tests, or ensuring code quality through testing.
| name | pest-testing |
| description | Comprehensive guidance for writing Pest v4 tests in Laravel applications, including feature tests, unit tests, and browser tests. Use this skill when writing tests, implementing test-driven development, testing APIs, creating browser automation tests, or ensuring code quality through testing. |
This skill provides expert guidance for writing high-quality tests with Pest v4 in Laravel applications, covering feature tests, unit tests, browser tests, and testing best practices.
Provide comprehensive Pest v4 testing guidance covering:
Use this skill when:
Focus on feature tests that verify complete workflows:
// ✅ GOOD - Feature test testing full workflow
it('creates a post', function () {
$user = User::factory()->create();
$response = $this->actingAs($user)->post('/posts', [
'title' => 'My Post',
'body' => 'Content',
]);
$response->assertCreated();
$this->assertDatabaseHas('posts', [
'title' => 'My Post',
'user_id' => $user->id,
]);
});
// Unit tests are for isolated logic
it('calculates total correctly', function () {
$calculator = new Calculator();
expect($calculator->add(2, 2))->toBe(4);
});
Never manually create models - use factories:
// ✅ CORRECT - Use factories
$user = User::factory()->create();
$posts = Post::factory()->count(3)->create();
// Check for factory states
$admin = User::factory()->admin()->create();
$publishedPost = Post::factory()->published()->create();
// ❌ WRONG - Manual creation
$user = User::create([
'name' => 'Test',
'email' => 'test@example.com',
// ... many fields
]);
When testing similar scenarios with different data, use datasets:
// ✅ GOOD - Using dataset
it('validates email format', function (string $email, bool $valid) {
$validator = validator(['email' => $email], ['email' => 'email']);
expect($validator->passes())->toBe($valid);
})->with([
['valid@example.com', true],
['invalid', false],
['test@test.co', true],
['@example.com', false],
]);
// ❌ WRONG - Duplicate tests
it('accepts valid email', function () {
$validator = validator(['email' => 'valid@example.com'], ['email' => 'email']);
expect($validator->passes())->toBeTrue();
});
it('rejects invalid email', function () {
$validator = validator(['email' => 'invalid'], ['email' => 'email']);
expect($validator->passes())->toBeFalse();
});
Prefer specific assertions over generic ones:
// ✅ GOOD - Specific assertions
$response->assertOk(); // 200
$response->assertCreated(); // 201
$response->assertNoContent(); // 204
$response->assertNotFound(); // 404
$response->assertForbidden(); // 403
$response->assertUnprocessable();// 422
// ❌ AVOID - Generic assertions
$response->assertStatus(200);
$response->assertStatus(404);
Always import the mock function before using it:
use function Pest\Laravel\mock;
it('mocks a service', function () {
$mock = mock(PaymentService::class);
$mock->shouldReceive('charge')
->once()
->andReturn(true);
// Test code
});
Read references/core.md for complete Pest syntax and expectations API.
php artisan test
php artisan test tests/Feature/PostTest.php
php artisan test --filter=login
php artisan test --filter="can create posts"
php artisan test --group=integration
Best Practice: Run the minimal number of tests using an appropriate filter when developing, then run the full suite before committing.
it('displays homepage', function () {
$response = $this->get('/');
$response->assertOk()
->assertSee('Welcome');
});
it('creates resource', function () {
$user = User::factory()->create();
$response = $this->actingAs($user)->post('/posts', [
'title' => 'My Post',
'body' => 'Content',
]);
$response->assertCreated()
->assertJson(['title' => 'My Post']);
});
it('requires authentication', function () {
$response = $this->get('/dashboard');
$response->assertRedirect('/login');
});
it('allows authenticated users', function () {
$user = User::factory()->create();
$response = $this->actingAs($user)->get('/dashboard');
$response->assertOk();
});
it('validates required fields', function () {
$user = User::factory()->create();
$response = $this->actingAs($user)->post('/posts', []);
$response->assertUnprocessable()
->assertJsonValidationErrors(['title', 'body']);
});
Read references/laravel.md for comprehensive Laravel testing patterns.
Pest v4 introduces powerful browser testing capabilities:
use function Pest\Laravel\visit;
it('can login', function () {
$user = User::factory()->create([
'password' => bcrypt('password'),
]);
$page = visit('/login');
$page->fill('email', $user->email)
->fill('password', 'password')
->click('Login')
->assertPath('/dashboard')
->assertSee("Welcome, {$user->name}");
});
it('has no JavaScript errors', function () {
$pages = visit(['/', '/about', '/contact']);
$pages->assertNoJavascriptErrors()
->assertNoConsoleLogs();
});
it('works in dark mode', function () {
$page = visit('/', colorScheme: 'dark');
$page->assertSee('Welcome')
->assertNoJavascriptErrors();
});
it('works on mobile', function () {
$page = visit('/', device: 'iPhone 14 Pro');
$page->assertSee('Welcome')
->assertVisible('.mobile-menu');
});
Read references/browser.md for comprehensive browser testing guide.
// Single model
$user = User::factory()->create();
// Multiple models
$users = User::factory()->count(5)->create();
// With specific attributes
$user = User::factory()->create([
'name' => 'John Doe',
'email' => 'john@example.com',
]);
// With relationships
$user = User::factory()
->has(Post::factory()->count(3))
->create();
Check if factories have custom states before manually setting attributes:
// Check factory for states like:
$admin = User::factory()->admin()->create();
$publishedPost = Post::factory()->published()->create();
$verifiedUser = User::factory()->verified()->create();
Clean database state between tests:
use Illuminate\Foundation\Testing\RefreshDatabase;
uses(RefreshDatabase::class);
it('creates user', function () {
$user = User::factory()->create();
expect(User::count())->toBe(1);
});
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Queue;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Notification;
use Illuminate\Support\Facades\Storage;
it('sends email', function () {
Mail::fake();
// Trigger email
Mail::assertSent(WelcomeEmail::class);
});
it('dispatches job', function () {
Queue::fake();
// Trigger job
Queue::assertPushed(ProcessPodcast::class);
});
it('dispatches event', function () {
Event::fake();
// Trigger event
Event::assertDispatched(UserCreated::class);
});
use function Pest\Laravel\mock;
it('mocks external service', function () {
$mock = mock(PaymentGateway::class);
$mock->shouldReceive('charge')
->once()
->with(100)
->andReturn(['status' => 'success']);
// Test code that uses PaymentGateway
});
describe('User Management', function () {
it('creates users', function () {
//
});
it('updates users', function () {
//
});
it('deletes users', function () {
//
});
});
it('is an integration test', function () {
//
})->group('integration');
it('is slow', function () {
//
})->group('slow', 'integration');
// Run: php artisan test --group=integration
This skill includes detailed reference files:
references/core.md - Pest syntax, expectations API, assertions, datasets, mocking, lifecycle hooksreferences/browser.md - Browser testing, interactions, waiting, device testing, screenshots, smoke testingreferences/laravel.md - HTTP testing, authentication, validation, database testing, faking servicesRead the appropriate reference file(s) when working on specific testing tasks.
assertOk() not assertStatus(200)use function Pest\Laravel\mock;assertNoJavascriptErrors()php artisan make:test FeatureTest --pestphp artisan test --filter=FeatureNamephp artisan testtests/Browser/This skill ensures tests are comprehensive, maintainable, and follow Pest v4 best practices for Laravel applications.