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 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...