【发布时间】:2016-08-24 18:08:16
【问题描述】:
当依赖的数据集有错误时,如何让 PHPUnit 跳过测试?
作品
如果我的数据提供者只有导致错误的东西,那么它将适当地跳过相关测试。 注意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.
不工作
但是,如果我向提供程序添加一个好的数据,那么尽管其余部分导致错误,PHPUnit 仍会继续执行 所有 依赖测试(甚至相应的有错误的数据集)。它不会跳过任何东西。 注意已将['real_file.txt'], 添加到数据提供者。
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 在使用@dataProvider 时不会跳过@depends 错误测试
来自their docs:
注意
当测试依赖于使用数据提供者的测试时,依赖的测试将在它所依赖的测试对至少一个数据集成功时执行。
如果依赖测试中提供的数据的任何部分导致错误,我想一起跳过一些测试。有没有办法解决这个限制?
如果需要,您可以fork these files 进行快速测试,或者直接克隆:
git clone https://github.com/admonkey/phpunittest.git
【问题讨论】:
-
有趣的想法。问题不在于他忽略了
@depends- 他只是很满意其中一项数据测试有效......
标签: php unit-testing testing phpunit