gpt4 book ai didi

php - 如何跳过 PHPUnit 中的错误测试?

转载 作者:行者123 更新时间:2023-11-28 20:28:52 26 4
gpt4 key购买 nike

当相关的数据集出现错误时,如何让 PHPUnit 跳过测试?


作品

如果我的数据提供者只有导致错误的东西,那么它会适本地跳过依赖测试。 注意 已跳过: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 不会跳过 @depends使用 @dataProvider 时的错误测试

来自 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.

如果依赖测试中提供的数据的任何部分导致错误,我想一起跳过一些测试。有什么方法可以解决此限制?


您可以 fork these files如果需要,可以进行快速测试,或者只是克隆:

git clone https://github.com/admonkey/phpunittest.git

最佳答案

也许这是您期望的行为:

<?php

class DataProviderDependsTest extends PHPUnit_Framework_TestCase
{
protected static $failed = false;

public function getDataProvider() {
return [
['real_file.txt'],
['non-existent_file.txt'],
];
}

/**
* @dataProvider getDataProvider
*/
public function testCanBeDependedOn($data) {
try {
$actual = file_get_contents($data);
self::assertSame('expected', $actual);
} catch(Exception $e) {
self::$failed = true;
throw $e;
}
}

/**
* @dataProvider getDataProvider
* @depends testCanBeDependedOn
*/
public function testCanDepend($data) {
if (self::$failed) {
self::markTestSkipped('testCanBeDependedOn failed');
}
self::assertTrue(true);
}
}

关于php - 如何跳过 PHPUnit 中的错误测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39130244/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com