Unit Testing
Also Known As
unit tests
PHPUnit
isolated testing
TL;DR
Automated tests that verify individual units of code (classes, methods) in isolation from external dependencies.
Explanation
Unit tests isolate a single class or function, replacing real dependencies (databases, HTTP clients, file systems) with test doubles (mocks, stubs, fakes). They run fast (no I/O), pinpoint failures precisely, and are the foundation of test-driven development. PHPUnit is the standard PHP unit testing framework. Well-designed code (injected dependencies, small classes) is easy to unit test — if a class is hard to test, that's a signal its design needs improvement. Aim for high coverage of business logic, edge cases, and error paths.
Common Misconception
✗ A unit test tests a single function or method in isolation. A unit is the smallest testable piece of behaviour — which may span multiple methods. The defining characteristic is speed and isolation from external dependencies, not strict single-method scope.
Why It Matters
Unit tests verify individual functions and methods in isolation — they run in milliseconds, pinpoint exactly which unit is broken, and provide a safety net for refactoring without fear of regression.
Common Mistakes
- Tests that test multiple units — failing tests give no indication of which unit is broken.
- Tests with no assertions — they pass always and provide false confidence.
- Tests tightly coupled to implementation details — refactoring breaks tests even when behaviour is correct.
- No tests for edge cases (empty input, null, zero, max values) — bugs live in boundaries.
Code Examples
✗ Vulnerable
// Test with no assertion — always passes:
public function testProcess(): void {
$service = new OrderService();
$service->process($order); // Called but nothing asserted
// PHPUnit reports: 1 test, 0 assertions — misleadingly green
}
// With assertions:
public function testProcess(): void {
$result = $service->process($order);
$this->assertSame('processed', $result->status);
$this->assertTrue($result->emailSent);
}
✓ Fixed
use PHPUnit\Framework\TestCase;
class MoneyTest extends TestCase {
public function test_add_same_currency(): void {
$a = new Money(100, 'GBP');
$b = new Money(50, 'GBP');
$this->assertEquals(new Money(150, 'GBP'), $a->add($b));
}
public function test_add_different_currency_throws(): void {
$this->expectException(CurrencyMismatch::class);
(new Money(100, 'GBP'))->add(new Money(100, 'USD'));
}
}
Tags
🤝 Adopt this term
£79/year · your link shown here
Added
15 Mar 2026
Edited
22 Mar 2026
Views
36
🤖 AI Guestbook educational data only
|
|
Last 30 days
Agents 1
Amazonbot 10
Perplexity 10
Ahrefs 3
Unknown AI 2
Majestic 1
Google 1
Qwen 1
SEMrush 1
Also referenced
How they use it
crawler 29
Related categories
⚡
DEV INTEL
Tools & Severity
🟡 Medium
⚙ Fix effort: Medium
⚡ Quick Fix
Test one unit of behaviour per test, arrange-act-assert pattern, inject all dependencies so they can be mocked — test the what not the how
📦 Applies To
PHP 5.0+
web
cli
queue-worker
🔗 Prerequisites
🔍 Detection Hints
No test directory or test coverage <40% or tests hitting real database/filesystem/network
Auto-detectable:
✓ Yes
phpunit
pest
phpstan
infection
⚠ Related Problems
🤖 AI Agent
Confidence: Medium
False Positives: Medium
✗ Manual fix
Fix: Medium
Context: File
Tests: Update