扩展PHPUnit时可捕获的致命错误

时间:2014-06-06 12:50:48

标签: php phpunit phar

首先,我的开发计算机无法访问互联网,因此我无法从PEAR或Composer安装PHPUnit,然后我只需下载.PHAR存档。

然后我用PHPUnit类继承写了一个类:

require_once './TU/PHPUnit/phpunit.phar';
class A_TEST extends PHPUnit_Framework_TestSuite {

    public static function myRunFunction(){}

}

当我调用我的对象$t = new A_TEST();时,我收到以下错误:

#!/usr/bin/env php
Notice: Undefined index: argv in phar://C:/Program Files/myPath/TU/PHPUnit/phpunit.phar/phpunit/TextUI/Command.php on line 132

Catchable fatal error: Argument 1 passed to PHPUnit_TextUI_Command::run() must be an array, null given, called in phar://C:/Program Files/myPath/TU/PHPUnit/phpunit.phar/phpunit/TextUI/Command.php on line 132
and defined in phar://C:/Program Files/myPath/TU/PHPUnit/phpunit.phar/phpunit/TextUI/Command.php on line 139

是否还有其他配置可以使其正常工作?

1 个答案:

答案 0 :(得分:2)

PHPUnit意味着从命令行运行。

错误消息表明您没有使用PHPUnit"可执行文件"它的设计方式。 (缺少argv条目,应该从命令行环境传递下来)

使用默认配置php -f path/to/phpunit.phar path/to/tests运行测试应该足够了。

可以使用command line optionsconfiguration file进行配置。

请注意,phpunit拥有自己的命名方案。 (测试前缀为test的方法,测试调用后缀为Test)。

无论如何,您通常不需要自己创建测试实例。 PHPUnit做了步法。

您的示例也不是使用PHPUnit_Framework_TestSuite的常用方法。如果要自己处理,通常将测试或对测试类的引用作为构造函数参数传递。我建议使用配置文件来创建套装,因为它更容易管理xml配置,并且不需要任何其他代码,导致错误,未经测试的代码或至少不必要的样板)

$suite = new PHPUnit_Framework_TestSuite;
$suite->addTest(new MyTest('testEpicStuff'));

$suite = new PHPUnit_Framework_TestSuite(
    new ReflectionClass('MyTest') // run all tests in the class
);