Test Naming Conventions
Also Known As
test method naming
BDD test names
test description
TL;DR
Well-named tests read as specifications — they document what the system does, make failure messages self-explanatory, and allow filtering tests by feature or scenario.
Explanation
PHPUnit test methods should describe the scenario being tested, not the method under test. Pattern: testMethodName is poor (what scenario?); testUserCannotLoginWithWrongPassword is good (describes behaviour). BDD-style: it_rejects_login_with_wrong_password or givenActiveUser_whenWrongPassword_thenAccessDenied (Given/When/Then). The test name should make the failure message self-explanatory — if it fails you know exactly what broke without reading the test body.
Common Misconception
✗ testMethodName() is a sufficient test name — it names the method under test, not the scenario; when the test fails, 'testCalculateTotal failed' tells you nothing about what went wrong.
Why It Matters
A failed test named testUserCannotPurchaseWithExpiredCreditCard is immediately actionable; a failed test named testCheckout requires reading the test body before you understand what broke.
Common Mistakes
- test + method name: testGetUser() — what scenario? What input? What expected outcome?
- Overly generic names: testHappyPath(), testEdgeCase() — not descriptive.
- Not using @test annotation with natural language: /** @test */ public function user_cannot_login_with_wrong_password().
- Names that describe implementation: testCallsDatabaseQueryOnce() — describe behaviour, not internals.
Code Examples
✗ Vulnerable
// Useless test names — failures are opaque:
public function testCalculate(): void { /* What calculation? What case? */ }
public function testUser(): void { /* What about a user? */ }
public function testHappyPath(): void { /* Every test is a happy path */ }
public function testGetUserById(): void { /* What scenario? Exists? Missing? */ }
✓ Fixed
// Self-documenting test names:
public function testCalculateTotalAppliesVatCorrectlyForUkCustomers(): void { }
public function testGuestUserCannotAccessAdminPanel(): void { }
public function testOrderStatusChangesToProcessingAfterPaymentConfirmed(): void { }
public function testFindByIdReturnsNullWhenUserDoesNotExist(): void { }
// Or BDD-style with @test:
/** @test */
public function it_rejects_checkout_when_cart_is_empty(): void { }
Tags
🤝 Adopt this term
£79/year · your link shown here
Added
16 Mar 2026
Edited
22 Mar 2026
Views
21
🤖 AI Guestbook educational data only
|
|
Last 30 days
Agents 0
No pings yet today
No pings yesterday
Perplexity 7
Amazonbot 6
Google 5
Unknown AI 3
Ahrefs 1
Also referenced
How they use it
crawler 18
crawler_json 3
pre-tracking 1
Related categories
⚡
DEV INTEL
Tools & Severity
🟢 Low
⚙ Fix effort: Low
⚡ Quick Fix
Name tests as sentences: test_it_rejects_orders_above_credit_limit() or 'it rejects orders above credit limit' in Pest — the test name should be the failing assertion message
📦 Applies To
any
web
cli
queue-worker
🔗 Prerequisites
🔍 Detection Hints
testSomething() generic test names; testMethod1() testMethod2(); test names that don't describe behaviour
Auto-detectable:
✗ No
phpunit
pest
⚠ Related Problems
🤖 AI Agent
Confidence: Medium
False Positives: Low
✗ Manual fix
Fix: Low
Context: File