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 mock a method with different arguments in PHPUnit?
- How to ignore a test in PHPUnit?
- How to show warnings in PHPUnit?
- How to mock a static method with PHPUnit?
- How to check if a JSON contains a value in PHPUnit?
- How to test private methods in PHPUnit?
- How to stop PHPUnit on failure?
- How to generate a coverage report with PHPUnit?
- How to install PHPUnit with a PHAR file?
See more codes...