My setup is 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'd like to focus on testTwo but when I do phpunit --filter testTwo
I get message like this:
This test depends on "MyTest::testOne" to pass.
No tests executed!
My question: Is there a way to run one test with all its dependencies?
There's not out of the box way to run automatically all the dependencies. You can however put your tests in groups with the @group
annotation and then run phpunit --group myGroup
.
I know, this is also not much convenient, but you can try
phpunit --filter 'testOne|testTwo'
According to phpunit docs we can use regexps as filter.
Also you may consider using data provider to generate your value for the second test. But be aware that data provider method will always be executed before all tests so it may slow down the execution if it has any heavy processing.
One more approach is to create some helper method or object that will do some actual job and cache results to be used by various tests. Then you won't need to use dependencies and your data will be generated on request and cached to be shared by different 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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With