Appearance
Test
Structure
- Tests should mirror the directory structure of the code they're testing
- Use descriptive test method names that explain the scenario being tested
- Use the
CoversClassPHPUnit attribute to indicate which classes are being tested - Extend
Tests\ApplicationTestCasefor feature tests (when the Laravel container needed) - Extend
PHPUnit\Framework\TestCaseclass for simple unit tests - Do not add comments like
// Arrange, please separate all AAA blocks by empty lines
Test Data
Create minimal data required for the test
Minimize usages of
setUp()method, prefer private methods insteadTo test datasets, prefer Data Providers and
PHPUnit\Framework\Attributes\TestWithattributeUse Generators for Data Providers. Use string keys for better DX
Use PHPUnit attributes like
#[PHPUnit\Framework\Attributes\Test],#[Group]instead of PHPDoc annotations (import them)Directly use Factory classes (located at the
tests/Factoriesdirectory):diff- User::factory()->make(); + UserFactory::make();Use
createOne()Factory method to create a single ModelStart test names from
it_
Assertions
One assertion per line
diff- $response->assertOk()->assertJsonCount(2); + $response->assertOk(); + $response->assertJsonCount(2);
HTTP Tests
Use named routes (if available) or
action()helper with controller's FQCN instead of hardcoded URL:diff- $this->postJson("/api/v1/users/resend-email-verification", [...]); + $this->postJson(action(ResendEmeilVerificationController::class), [...]);or:
diff- $this->postJson("/api/v1/users/resend-email-verification", [...]); + $this->postJson(route("users.resend-email-verification"), [...]);