phpunitHow to use an inline dataprovider in PHPUnit?
Inline dataproviders are used to provide data to a test method in PHPUnit. It is a simple way to pass multiple sets of data to a test method.
Example code
public function provider()
{
return [
[1, 2, 3],
[4, 5, 9],
[7, 8, 15]
];
}
/**
* @dataProvider provider
*/
public function testAdd($a, $b, $expected)
{
$this->assertEquals($expected, $a + $b);
}
Output example
OK (3 tests, 3 assertions)
Code explanation
public function provider()
: This is the function that returns the data to be used in the test method.@dataProvider provider
: This is the annotation that tells PHPUnit which function to use as the data provider.$this->assertEquals($expected, $a + $b)
: This is the assertion that is used to check the result of the test.
Helpful links
More of Phpunit
- How to skip a PHPUnit test?
- How to run tests in parallel with PHPUnit?
- How to use hooks in PHPUnit?
- What are PHPUnit required extensions
- How to use the PHPUnit Framework TestCase?
- How to run PHPUnit in quiet mode?
- How to show warnings in PHPUnit?
- How to load fixtures with PHPUnit?
- How to use a listener with PHPUnit?
- How to stop PHPUnit on failure?
See more codes...