phpunitHow to use fixtures with PHPUnit?
Fixtures are used to provide a known set of data to test against. PHPUnit provides a DataProvider
annotation to allow for the creation of fixtures.
Example code
<?php
use PHPUnit\Framework\TestCase;
class MyTest extends TestCase
{
/**
* @dataProvider fixtureProvider
*/
public function testFixture($fixture)
{
// Test code here
}
public function fixtureProvider()
{
return [
[1],
[2],
[3],
];
}
}
Output example
PHPUnit 8.5.8 by Sebastian Bergmann and contributors.
. 3 / 3 (100%)
Time: 00:00.001, Memory: 6.00 MB
OK (3 tests, 3 assertions)
The DataProvider
annotation is used to specify a method that will provide the data for the test. The method should return an array of arrays, with each inner array containing the data for a single test.
The example code creates a test class with a testFixture
method that is annotated with the @dataProvider
annotation. The fixtureProvider
method is used to provide the data for the test. The fixtureProvider
method returns an array of arrays, each containing a single value.
Helpful links
More of Phpunit
- How to show warnings in PHPUnit?
- How to stop PHPUnit on failure?
- How to skip a PHPUnit test?
- How to use the PHPUnit Framework TestCase?
- How to ignore a test in PHPUnit?
- How to use getMockBuilder with PHPUnit?
- How to use dependency injection in PHPUnit?
- How to generate a coverage report with PHPUnit?
- How to run all PHPUnit tests?
- How to install PHPUnit with a PHAR file?
See more codes...