testing-best-practices

Guides Laravel test design, coverage selection, assertions, isolation, and review using PHPUnit conventions.

1|Updated Aug 29, 2026
One-click install
npx skills add https://github.com/IzzatFirdaus/task-enterprise-api --skill testing-best-practices-izzatfirdaus
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: testing-best-practices
Source: https://github.com/IzzatFirdaus/task-enterprise-api/tree/main/.github/skills/testing-best-practices
Command: npx skills add https://github.com/IzzatFirdaus/task-enterprise-api --skill testing-best-practices-izzatfirdaus

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Writing Laravel tests that are valuable, deterministic, and maintainable is hard: tests often duplicate coverage, assert implementation details instead of behavior, or depend on time, randomness, and network calls. This Skill provides a structured rule set for designing, writing, and reviewing PHPUnit-based Laravel tests so each test detects a distinct defect. ## Core Features & Use Cases - Coverage and Naming Rules: Decide what to test (decisions, failure modes, contracts), name tests as behavior specifications, and structure files to mirror the class under test. - Assertion and Endpoint Guidance: Choose subject-specific Laravel and PHPUnit assertions, cover authentication, authorization, tenant isolation, and validation for HTTP endpoints. - Isolation and Performance: Control fakes, mocks, outbound HTTP, time, randomness, and databases, plus suite-level performance settings like BCRYPT_ROUNDS and parallel runs. - Use Case: When adding a new API endpoint to a Laravel app, use this Skill to write feature tests covering auth, validation, and persisted state, then review the suite against the review checklist. ## Quick Start Ask the assistant to write or review tests for a Laravel class or endpoint using the testing-best-practices rules, for example: write feature tests for the task creation endpoint covering authentication, validation, and database state.

Frequently Asked Questions about testing-best-practices

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I write good feature tests for Laravel API endpoints?▼

Write one feature test per applicable case: missing authentication, insufficient role, failed validation, and the valid request. Assert the complete result including the response, database state, and dispatched jobs or events, using named Laravel response assertions.

What is the difference between assertSame and assertEquals in PHPUnit?▼

assertSame compares both value and type, while assertEquals only compares values loosely. Use assertSame for return values and object state so type mismatches fail the test instead of passing silently.

Should I use Pest or PHPUnit for Laravel testing?▼

This project uses PHPUnit, so follow PHPUnit conventions such as the test_ prefix or #[Test] attribute and PHPUnit data providers. Project conventions take precedence, so match the syntax used by nearby tests in the same directory.

How do I mock HTTP requests in Laravel tests?▼

Call Http::preventStrayRequests() so unmatched requests fail, then fake the exact endpoint each test uses with Http::fake(). Avoid faking without an endpoint because it accepts unexpected requests and can hide defects.

Why is my Laravel test suite slow and how do I fix it?▼

Common causes are the default BCRYPT_ROUNDS value of 12, enabled XDebug, and real sleep or network calls. Set BCRYPT_ROUNDS=4 in .env.testing, disable XDebug, fake Sleep and HTTP, and run php artisan test --parallel with ParaTest.

When should I write a unit test instead of a feature test in Laravel?▼

Write a feature test first for every behavior reachable through a request. Write a unit test only for logic that does not use the framework, such as a pure value object or calculation class.