Data Providers for Parameterized Tests
Run the same test with multiple inputs using dataProvider.
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],
];
}All lessons in this course
- PHPUnit Setup and First Test
- Assertions and Test Methods
- Data Providers for Parameterized Tests
- Mocking Dependencies with PHPUnit