Parameterised / Data-Driven Tests
Also Known As
dataProvider
data-driven tests
parameterised tests
test matrix
TL;DR
Running the same test logic with multiple input/output pairs — PHPUnit's @dataProvider eliminates copy-pasted test methods for the same behaviour with different values.
Explanation
A data provider is a static method returning an array of arrays (or a generator) — each inner array is one test case with its named label. PHPUnit runs the test method once per dataset, naming failures with the dataset key. Use data providers for: boundary values, equivalence classes, valid/invalid input sets, and all-combinations testing. Keep the test logic in the test method; keep the data in the provider. Pest PHP offers ->with() for the same concept with cleaner syntax.
Common Misconception
✗ A separate test method per case is clearer than a data provider — 20 test methods testing the same logic with different inputs is 20× the maintenance when the logic changes.
Why It Matters
Data providers make test matrices exhaustive without duplication — testing all valid/invalid boundary combinations becomes trivial rather than a maintenance burden.
Common Mistakes
- Data provider method not declared static — PHPUnit requires static data providers.
- Not naming dataset keys — assertX failed with data set #3 is unhelpful; use 'valid UK postcode' as the key.
- Complex logic in the data provider — providers should only return data, not build objects.
- One data provider shared across unrelated tests — keep providers focused on one test method.
Code Examples
✗ Vulnerable
// Copy-pasted test methods — O(n) maintenance:
public function testValidEmailAccepted(): void {
$this->assertTrue(validateEmail('user@example.com'));
}
public function testInvalidEmailWithNoAt(): void {
$this->assertFalse(validateEmail('userexample.com'));
}
public function testInvalidEmailWithNoTld(): void {
$this->assertFalse(validateEmail('user@example'));
}
// ...15 more identical methods
✓ Fixed
/** @dataProvider emailProvider */
public function testEmailValidation(string $email, bool $expected): void {
$this->assertSame($expected, validateEmail($email));
}
public static function emailProvider(): array {
return [
'valid standard email' => ['user@example.com', true],
'valid with subdomain' => ['u@sub.example.com', true],
'invalid no at sign' => ['userexample.com', false],
'invalid no TLD' => ['user@example', false],
'invalid consecutive dots' => ['user..name@ex.com', false],
];
}
Tags
🤝 Adopt this term
£79/year · your link shown here
Added
16 Mar 2026
Edited
22 Mar 2026
Views
26
🤖 AI Guestbook educational data only
|
|
Last 30 days
Agents 0
No pings yet today
No pings yesterday
Perplexity 8
Amazonbot 6
Unknown AI 3
Ahrefs 2
SEMrush 2
Majestic 1
Google 1
Also referenced
How they use it
crawler 22
pre-tracking 1
Related categories
⚡
DEV INTEL
Tools & Severity
🟢 Low
⚙ Fix effort: Low
⚡ Quick Fix
Replace 5 near-identical test methods with one @dataProvider or Pest ->with() that covers all cases — it's easier to add new cases and the pattern scales to edge cases
📦 Applies To
PHP 5.0+
web
cli
queue-worker
🔗 Prerequisites
🔍 Detection Hints
Multiple test methods differing only in input/output data; copy-pasted test structure with different literals
Auto-detectable:
✗ No
phpunit
pest
⚠ Related Problems
🤖 AI Agent
Confidence: Low
False Positives: Medium
✗ Manual fix
Fix: Low
Context: Function
Tests: Update