使用PHPUnit,使用数据提供程序,最后一个数据集始终显示“测试用例意外终止”

时间:2012-05-18 13:13:01

标签: php testing phpunit zend-studio

所以我使用PHPUnit进行测试。尝试将DataProvider与我的一个测试一起使用。

/**
 * Tests Events_Event->Events_Event()
 * @dataProvider provider
 */
public function testEvents_Event($Name, $param, $time) {
        //$this->assertInstanceOf("Events_Event", $this->Events_Event->Events_Event("test2", array()));
        $this->assertTrue(true);
    }

public static function provider()
    {
        return array(
            array("test", array("Like a boss"), "Cheack the time"),
            array("test2", array("Like a boss"), "9:00"),
            array("test3", array("Time to go home"), "4:00"),
            array("test3", array("Time to go home"), "4:00")
            );
    }

结果:

testEvents_Event with data set#0
testEvents_Event with data set#1
testEvents_Event with data set#2
testEvents_Event with data set#3: The test case was unexpectedly terminated

这发生在最后一个数据集上,无论有多少数据集以及最后一个数据集是否有效。正如您所看到的,我们已将测试简化为简单$this->assertTrue(true),但它仍然给我们错误。

我们需要做些什么才能让数据提供商工作?

如果重要的是我在Zend Studio 9.0.3中使用PHPUnit,我已经检查了更新,它告诉我所有内容都是最新的。

2 个答案:

答案 0 :(得分:1)

我正在经历

....

时间:0秒,内存:12.75Mb

好(4个测试,0个断言)

/**
 * Tests Events_Event->Events_Event()
 * @dataProvider provider
 */
public function testEvents_Event($Name, $param, $time)
{

}

public static function provider()
{
    return array(
        array("test", array("Like a boss"), "Cheack the time"),
        array("test2", array("Like a boss"), "9:00"),
        array("test3", array("Time to go home"), "4:00"),
        array("test3", array("Time to go home"), "4:00")
        );
}

如何运行测试?没有任何其他依赖? 测试是通过任何IDE运行的吗?

答案 1 :(得分:1)

PHPUnit为每个数据提供者方法实例化测试用例。由于PHP的魔法,您可以使用静态数据提供程序方法,但它们是使用实例调用的,因此应该是非静态的。

如果您的测试用例有构造函数,则它必须接受三个参数(请参阅PHPUnit_Framework_TestCase的源代码)并将它们传递给父构造函数。其中之一是来自特定测试的提供者的数据。

但是,我怀疑这些是问题所在。我的钱在ZendStudio上以及它如何解析PHPUnit的输出,正如戈登建议的那样。从命令行运行此测试用例时,您是否看到了同样的问题?