How do I get PHPUnit to skip tests when the dependent one has an error with a data set?
Works
If my data provider only has things that cause errors, then it will appropriately skip the dependent tests. Note the Skipped: 1
class DataProviderDependsTest extends PHPUnit_Framework_TestCase
{
public function getDataProvider(){
return [
['non-existent_file.txt'],
];
}
/**
* @dataProvider getDataProvider
*/
public function testCanBeDependedOn($data){
$actual = file_get_contents($data);
$this->assertSame('expected',$actual);
}
/**
* @dataProvider getDataProvider
* @depends testCanBeDependedOn
*/
public function testCanDepend($data){
$this->assertTrue(false);
}
}
PHPUnit 5.5.0 by Sebastian Bergmann and contributors. ES 2 / 2 (100%) Time: 28 ms, Memory: 4.00MB There was 1 error: 1) DataProviderDependsTest::testCanBeDependedOn with data set #0 ('non-existent_file.txt') file_get_contents(non-existent_file.txt): failed to open stream: No such file or directory /home/xenial/phpunittest/test.php:16 ERRORS! Tests: 1, Assertions: 0, Errors: 1, Skipped: 1.
Does Not Work
However, if I add one piece of good data to the provider, then despite the errors caused by the rest, PHPUnit proceeds to execute all the dependent tests anyway (even the corresponding data sets with errors). It doesn't skip anything. Note the added ['real_file.txt'],
to the data provider.
class DataProviderDependsTest extends PHPUnit_Framework_TestCase
{
public function getDataProvider(){
return [
['real_file.txt'],
['non-existent_file.txt'],
];
}
/**
* @dataProvider getDataProvider
*/
public function testCanBeDependedOn($data){
$actual = file_get_contents($data);
$this->assertSame('expected',$actual);
}
/**
* @dataProvider getDataProvider
* @depends testCanBeDependedOn
*/
public function testCanDepend($data){
$this->assertTrue(false);
}
}
PHPUnit 5.5.0 by Sebastian Bergmann and contributors. .EFF 4 / 4 (100%) Time: 19 ms, Memory: 4.00MB There was 1 error: 1) DataProviderDependsTest::testCanBeDependedOn with data set #1 ('non-existent_file.txt') file_get_contents(non-existent_file.txt): failed to open stream: No such file or directory /home/xenial/phpunittest/test.php:16 -- There were 2 failures: 1) DataProviderDependsTest::testCanDepend with data set #0 ('real_file.txt') Failed asserting that false is true. /home/xenial/phpunittest/test.php:25 2) DataProviderDependsTest::testCanDepend with data set #1 ('non-existent_file.txt') Failed asserting that false is true. /home/xenial/phpunittest/test.php:25 ERRORS! Tests: 4, Assertions: 3, Errors: 1, Failures: 2.
PHPUnit doesn't skip @depends
tests on error when using @dataProvider
From their docs:
Note
When a test depends on a test that uses data providers, the depending test will be executed when the test it depends upon is successful for at least one data set.
I would like to skip some tests all together if any part of the provided data in the depended test causes an error. Is there any way to workaround this limitation?
You can fork these files for quick tests if you want, or just clone:
git clone https://github.com/admonkey/phpunittest.git
@depends
- he is simply satisfied that one of the tests with the data worked...