2

我有一个看起来像这样的 PHPUnit 测试:

/**
 * @dataProvider provideSomeStuff
 */
public function testSomething($a, $b, $c)
{
    ...
}

/**
 * @dataProvider provideSomeStuff
 * @depends testSomething
 */
public function testSomethingElse($a, $b, $c)
{
    ...
}

/**
 * @depends testSomething
 */
public function testMoreStuff()
{
    ...
}

// Several more tests with the exact same setup as testMoreStuff

即使testSomething成功,所有依赖它的测试都会被跳过。PHPUnit 手册中的一些注释告知测试可以依赖于使用数据提供者的其他测试:

注意
当一个测试从@dataProvider 方法和它@depends 的一个或多个测试接收输入时,来自数据提供者的参数将在来自依赖测试的参数之前。

注意
当测试依赖于使用数据提供者的测试时,依赖的测试将在它依赖的测试对至少一个数据集成功时执行。使用数据提供者的测试结果不能注入到依赖测试中。

所以我不知道为什么它会跳过我所有的测试。我已经为此苦苦挣扎了几个小时,有人帮助我。这是完整的测试代码,以防问题不能从上面的伪代码中得出

测试结果截图:

测试结果

4

2 回答 2

2

这似乎是 phpunit 3.4.5 中的一个错误,但在 phpunit 3.4.12 中已修复。

以下是基于手册中的示例的最小示例。我在PHPUnit 3.4.5中得到了与你相同的行为,但在PHPUnit 3.6.11中我得到了 4 次通过。缩小范围,phpunit 3.4 变更日志说它已在 PHPUnit 3.4.12 中修复。

class DataTest extends PHPUnit_Framework_TestCase
{

/**
* @dataProvider provider
*/
public function testAdd($a, $b, $c)
{
$this->assertEquals($c, $a + $b);
}


/**
* @depends testAdd
*/
public function testAddAgain()
{
$this->assertEquals(5,3+2);
}

/** */
public function provider()
{
return array(
array(0, 0, 0),
array(0, 1, 1),
array(1, 0, 1),
);
}

}
于 2012-06-17T03:32:07.170 回答
-1

您必须在主方法之后定义依赖方法。

public function testSomething()
{
    $foo = [];
    //test something
    return $foo;
}

/** 
 * @depends testSomething
 */
public function testBar(array $foo)
{
    //more tests 
}
于 2015-12-29T20:19:53.320 回答