How to run PHPUnit test with its dependencies
My setup looks something like this:
class MyTest extends PHPUnit_Framework_TestCase
{
// More tests before
public function testOne()
{
// Assertions
return $value;
}
/**
* @depends testOne
*/
public function testTwo($value)
{
// Assertions
}
// More tests after
}
I would like to focus on testTwo, but when I do phpunit --filter testTwo
, I get a message like this:
This test depends on "MyTest::testOne" to pass.
No tests executed!
My question is, is there a way to run one test with all its dependencies?
source to share
I know this is not very convenient either, but you can try
phpunit --filter 'testOne|testTwo'
According to the phpunit docs, we can use regex as a filter.
Also you can use a data provider to generate your value for the second test. But keep in mind that the data provider method will always run before all tests so that it can slow down execution if it has heavy processing.
Another approach is to create some kind of helper method or object that will execute some actual work and cache results to be used by various tests. Then you won't need to use dependencies, and your data will be generated on demand and cached for sharing by various tests.
class MyTest extends PHPUnit_Framework_TestCase
{
protected function _helper($someParameter) {
static $resultsCache;
if(!isset($resultsCache[$someParameter])) {
// generate your $value based on parameters
$resultsCache[$someParameter] = $value;
}
return $resultsCache[$someParameter];
}
// More tests before
public function testOne()
{
$value = $this->_helper('my parameter');
// Assertions for $value
}
/**
*
*/
public function testTwo()
{
$value = $this->_helper('my parameter');
// Get another results using $value
// Assertions
}
// More tests after
}
source to share