Data Providers for Parameterized Tests
Run the same test with multiple inputs using dataProvider.
Data Providers for Parameterized Tests is a free PHP Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the PHP Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Are Data Providers?
A data provider is a method that returns a set of test inputs. PHPUnit calls the test method once per dataset, making it easy to test many input combinations without repeating code.
Basic Data Provider
The provider returns a 2D array. Each sub-array is one call to the test method.
<?php
public static function additionProvider(): array
{
return [
"two positives" => [2, 3, 5],
"negative" => [-1, 1, 0],
"zeros" => [0, 0, 0],
];
}Linking with Attribute (PHPUnit 10+)
Use the #[DataProvider] attribute to link a test to its provider.
<?php
use PHPUnit\Framework\Attributes\DataProvider;
#[DataProvider("additionProvider")]
public function test_add(int $a, int $b, int $expected): void
{
$this->assertSame($expected, (new Calculator())->add($a, $b));
}Named Datasets
Use associative keys in the provider array to give each dataset a descriptive name — these appear in the test output.
<?php
public static function emailProvider(): array
{
return [
"valid email" => ["user@example.com", true],
"missing @" => ["userexample.com", false],
"missing domain" => ["user@", false],
"international" => ["用户@例子.com", true],
];
}Returning Generators
Providers can return a Generator instead of an array to generate datasets lazily — useful for very large datasets.
<?php
public static function largeProvider(): Generator
{
for ($i = 1; $i <= 1000; $i++) {
yield "case $i" => [$i, $i * 2];
}
}External Data Sources
Load test data from CSV, JSON, or a database inside the provider method to drive tests with real-world data.
<?php
public static function csvProvider(): array
{
$rows = array_map("str_getcsv", file("tests/fixtures/data.csv"));
return array_map(fn($r) => [$r[0], (int)$r[1], (int)$r[2]], $rows);
}Multiple Data Providers
A test method can reference multiple providers by stacking attributes.
Provider Must Be Static
Data provider methods must be declared static because PHPUnit calls them before instantiating the test class.
Testing Edge Cases
Use providers to systematically cover: zero, negative numbers, empty strings, null, max int — wherever edge cases are likely to surface bugs.
Data Provider Output
Failures show the dataset name: CalculatorTest::test_add with data set "negative" ([-1, 1, 0]) — making it easy to pinpoint which case failed.
Combining Providers with setUp
Data providers and setUp can be combined. setUp runs before each iteration, giving each dataset a fresh object under test.
Performance Consideration
Providers run at suite-load time. Avoid expensive operations (HTTP calls, DB queries) in providers — prefer factories and fixtures.
Summary
Data providers eliminate test duplication. Use associative keys for descriptive failure messages. Mark providers as static and link them with the #[DataProvider] attribute.
Quick Check
What visibility modifier must a data provider have?
Frequently asked questions
Is the “Data Providers for Parameterized Tests” lesson free?
Yes — the full text of “Data Providers for Parameterized Tests” is free to read here on the web, and the PHP Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the PHP Academy course, upgrade to CoddyKit PRO.
What will I learn in “Data Providers for Parameterized Tests”?
Run the same test with multiple inputs using dataProvider. You practise PHP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start PHP Academy?
No prior experience is required. PHP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Data Providers for Parameterized Tests” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this PHP Academy lesson?
Yes. Every PHP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- PHPUnit Setup and First Test
- Assertions and Test Methods
- Data Providers for Parameterized Tests
- Mocking Dependencies with PHPUnit