Data Providers in PHPUnit 3.2
Starting with version 3.2, PHPUnit has support for TestNG-style data providers:
<?php
class DataTest extends PHPUnit_Framework_TestCase
{
protected static $myData = array(
array(0, 0, 0),
array(0, 1, 1),
array(1, 0, 1),
array(1, 1, 3)
);
public static function provider()
{
return self::$myData;
}
/**
* @dataProvider provider
*/
public function testAdd($a, $b, $c)
{
$this->assertEquals($c, $a + $b);
}
}
?>
@dataProvider annotation returns an array or an Iterator object that contain argument arrays. For each of these arrays a test object is created that will run the test method with the argument from the array:sb@vmware ~ % phpunit DataTest PHPUnit 3.2.0-dev by Sebastian Bergmann. ...F Time: 0 seconds There was 1 failure: 1) testAdd(DataTest) with data (1, 1, 3) Failed asserting that <integer:2> matches expected value <integer:3>. /home/sb/DataTest.php:21 FAILURES! Tests: 4, Failures: 1.
14/10/2007 at 18:50 Permalink
Thanks, Sebastian, for your amazing work on this project!
Reply
16/10/2007 at 19:21 Permalink
Very enjoyable. Thanks! I'm warming up to using PHPUnit. Very warm.
Reply
13/06/2008 at 11:46 Permalink
The @dataProvider annotation only be processed in PHPUnit_Util_Test::getProvidedData, and this method only be called when construct a TestCase with parameters in PHPUnit_Framework_TestSuite::createTest. I think this should be a bug.
Reply
14/06/2008 at 14:18 Permalink
Reply