【问题标题】:Why is phpunit skipping most of my tests even though their dependency succeeded?为什么 phpunit 跳过了我的大部分测试,即使它们的依赖成功了?
【发布时间】:2012-06-15 12:52:46
【问题描述】:

我有一个看起来像这样的 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 manual 中的一些注释告知测试可以依赖于使用数据提供程序的其他测试:

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

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

所以我不知道为什么它会跳过我的所有测试。我已经为此苦苦挣扎了几个小时,有人帮助我。 Here's the complete test code 万一问题不能从上述伪代码导出

测试结果截图:

【问题讨论】:

标签: php phpunit


【解决方案1】:

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

以下是基于手册中的示例的最小示例。我在 PHPUnit 3.4.5 中得到了与你相同的行为,但在 PHPUnit 3.6.11 中我得到了 4 次通过。 缩小范围,phpunit 3.4 changelog 表示它已在 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),
);
}

}

【讨论】:

  • 我正在使用 PHPUnit 3.6.10,但我仍然遇到这种行为
  • (您的屏幕截图显示为 3.4.5;我假设您的意思是从那时起您已经升级)您仍然遇到我上面发布的最小示例的问题吗?这就是完整的文件,只需在顶部添加一个<?php,将其保存为testdeps.php,然后在同一目录下使用phpunit testdeps.php 运行它。
  • 哦,哇,我没有意识到我在工作和家里有不同的版本。当我回到家时,我将不得不再次尝试运行测试。幸好我们中的一个人正在关注*竖起大拇指*
【解决方案2】:

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

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

/** 
 * @depends testSomething
 */
public function testBar(array $foo)
{
    //more tests 
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-05
    • 2020-09-10
    • 1970-01-01
    • 2017-05-24
    相关资源
    最近更新 更多