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

Naming Test Methods (Given/When/Then)

testing Beginner

TL;DR

Test method names should describe behaviour, not implementation — test_calculateTotal_givenDiscountedItems_returnsReducedPrice beats test_calculateTotal().

Explanation

Good test naming patterns: (1) Given/When/Then: givenExpiredToken_whenValidating_thenThrowsException. (2) Should: shouldThrowWhenTokenExpired. (3) Behaviour description: calculatesDiscountForPremiumUsers. Bad patterns: test1, testMethod, testCalculate. PHPUnit: method name is the test name in output. Use @test annotation or prefix test. Name should read as a specification — failing tests produce meaningful error messages. The name should say what the test verifies, not how. BDD frameworks (Behat, Pest) formalise this with descriptive strings.

Common Misconception

Test names only matter to developers reading the code — test names appear in CI failure output and are the primary way to understand what broke in production.

Why It Matters

Poorly named tests produce cryptic CI failure messages — 'FAILED: testCalculate' tells you nothing. 'FAILED: givenExpiredToken_whenValidating_thenThrowsException' tells you exactly what broke.

Common Mistakes

  • Using generic names: test1, testMethod, testSuccess.
  • Naming after implementation: testCallsDiscountService instead of testAppliesDiscountForPremiumUsers.
  • Not including the expected outcome in the name.

Code Examples

✗ Vulnerable
public function testCalculate(): void { }
public function testCalculate2(): void { }
public function testUserService(): void { }
✓ Fixed
// PHPUnit:
public function test_givenPremiumUser_whenCalculatingTotal_appliesDiscount(): void {}
public function test_givenExpiredCoupon_whenCheckingOut_throwsException(): void {}

// Pest:
it('applies discount for premium users', function() {});
it('throws when coupon is expired', function() {});

Added 23 Mar 2026
Views 56
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 1 ping S 1 ping S 2 pings M 1 ping T 0 pings W 1 ping T 1 ping F 1 ping S 0 pings S 0 pings M 1 ping T 0 pings W 0 pings T
No pings yet today
No pings yesterday
Google 14 Amazonbot 14 Perplexity 10 Unknown AI 4 ChatGPT 2 Majestic 2 Meta AI 1 Ahrefs 1
crawler 45 crawler_json 1 pre-tracking 2
DEV INTEL Tools & Severity
🟢 Low ⚙ Fix effort: Low
⚡ Quick Fix
Name tests as: test_givenContext_whenAction_thenOutcome or 'it does X when Y'. Include the scenario and expected behaviour. Never use generic names like test1 or testMethod.
📦 Applies To
web cli queue-worker PHPUnit Pest Jest Vitest
🔗 Prerequisites
🔍 Detection Hints
function test[A-Z]|function test\d
Auto-detectable: ✓ Yes phpcs eslint
🤖 AI Agent
Confidence: Medium False Positives: Medium ✗ Manual fix Fix: Low Context: File

✓ schema.org compliant