Fixture Reuse in PHPUnit 3.4
As mentioned earlier, PHPUnit 3.4 adds support for Test Dependencies as introduced by Kuhn et. al. in JExample: Exploiting Dependencies Between Tests to Improve Defect Localization.
Today I implemented fixture reuse based on test dependency information:
<?php
class StackTest extends PHPUnit_Framework_TestCase
{
public function testPush()
{
$stack = array();
$this->assertEquals(0, count($stack));
array_push($stack, 'foo');
$this->assertEquals('foo', $stack[count($stack)-1]);
$this->assertEquals(1, count($stack));
return $stack;
}
/**
* @depends testPush
*/
public function testPop(array $stack)
{
$this->assertEquals('foo', array_pop($stack));
$this->assertEquals(0, count($stack));
}
}
?>
Just like JExample, PHPUnit 3.4 introduces producer-consumer relationships to unit-testing.
- A producer is a test method that yields its unit under test as return value.
- A consumer is a test method that depends on one or more producers and their return values.
PHPUnit 3.4 skips any test method whose producer has failed and the return value of a producer is injected into its consumers.
