我正在尝试让 PHPUnit 工作;我以前没用过。如果我在 CLI 中运行 phpunit,它可以工作,但通过脚本(这是我正在寻找的)它不会。这是我得到的输出(注意 Class 后面的两个空格):
Fatal error: Uncaught exception 'ReflectionException' with message 'Class does not exist' in /usr/local/share/pear/PHPUnit/Util/Test.php:295
Stack trace:
#0 /usr/local/share/pear/PHPUnit/Util/Test.php(295): ReflectionClass->__construct('')
#1 /usr/local/share/pear/PHPUnit/Util/Test.php(576): PHPUnit_Util_Test::parseTestMethodAnnotations(false, false)
#2 /usr/local/share/pear/PHPUnit/Util/Test.php(350): PHPUnit_Util_Test::getBooleanAnnotationSetting(false, false, 'backupGlobals')
#3 /usr/local/share/pear/PHPUnit/Framework/TestSuite.php(458): PHPUnit_Util_Test::getBackupSettings(false, false)
#4 /usr/local/share/pear/PHPUnit/Framework/TestSuite.php(834): PHPUnit_Framework_TestSuite::createTest(Object(ReflectionClass), false)
#5 /usr/local/share/pear/PHPUnit/Framework/TestSuite.php(212): PHPUnit_Framework_TestSuite->addTestMethod(Object(ReflectionClass), Object(ReflectionMethod))
#6 /usr/local/share/pear/PHPUnit/Framework/TestSuite.php(315): PHPUnit_Framework_TestSuite->__construct(Object(ReflectionClass))
#7 /var/www/www.s in /usr/local/share/pear/PHPUnit/Util/Test.php on line 295
似乎类名在addTestMethod
. 这是我的代码:
<?php
require_once 'PHPUnit/Autoload.php';
class MyTestCase extends PHPUnit_Framework_TestCase {
public function testSubtraction() {
$this->assertEquals(2 - 2, 0);
}
public function testAddition() {
$this->assertEquals(2 + 2, 4);
}
}
$c = new MyTestCase();
$suite = new PHPUnit_Framework_TestSuite();
$suite->addTestSuite('MyTestCase');
PHPUnit_TextUI_TestRunner::run($suite);
我设置错了吗?
以下是版本:
Installed packages, channel pear.phpunit.de:
============================================
Package Version State
File_Iterator 1.3.3 stable
PHPUnit 3.7.21 stable
PHPUnit_MockObject 1.2.3 stable
PHP_CodeCoverage 1.2.11 stable
PHP_Timer 1.0.4 stable
PHP_TokenStream 1.1.5 stable
Text_Template 1.1.4 stable
PHP 5.4
反射确实有效,我在自己的脚本中使用它。希望这与我的设置有关,我真的不想修改 PHPUnit ......当然它可以在其他地方工作。