← CodeClarityLab Home
Browse by Category
+ added · updated 7d
← Back to glossary

Test Naming Conventions

testing Beginner

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 { }

Added 16 Mar 2026
Edited 22 Mar 2026
Views 21
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings W 1 ping T 0 pings F 0 pings S 2 pings S 1 ping M 0 pings T 0 pings W 0 pings T 0 pings F 1 ping S 3 pings S 0 pings M 0 pings T 1 ping W 0 pings T 0 pings F 0 pings S 1 ping S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S 2 pings S 0 pings M 0 pings T 0 pings W 0 pings T
No pings yet today
No pings yesterday
Perplexity 7 Amazonbot 6 Google 5 Unknown AI 3 Ahrefs 1
crawler 18 crawler_json 3 pre-tracking 1
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

✓ schema.org compliant