如果你正确安装了phpunit(通过PEAR),那么就不需要包含文件;你只需要改变使用它测试php文件的方式(例如,你可以使用它来测试文件是否能够在浏览器中正常运行,输入localhost即可)。使用phpunit,你可以使用命令行;
第5章提供了使用命令行的相同示例(我认为这是一个标准)。因此,如果你已经正确安装了phpunit,你可以这样做:
File ExampleTest.php, located at the root of localhost (for me this is /var/www):
class ExampleTest extends PHPUnit_Framework_TestCase
{
public function testOne()
{
$this->assertTrue(FALSE);
}
}
Open a console (terminal on Mac or Linux, command prompt on Win), navigate to your localhost document root (where you saved ExampleTest.php) and type the following:
phpunit --verbose ExampleTest.php
You should see:
PHPUnit 3.4.13 by Sebastian Bergmann.
F
Time: 1 second, Memory: 6.50Mb
There was 1 failure:
1) ExampleTest::testOne
Failed asserting that <boolean:false> is true.
/var/www/ExampleTest.php:6
FAILURES!
Tests: 1, Assertions: 1, Failures: 1.
注意:以上所有内容均假定您已正确安装PHPUnit(如
第3章所述),并在此之后重新启动了Apache。
如果您确实希望在浏览器中运行测试,请使用以下代码。
ini_set('display_errors',1);
error_reporting(E_ALL|E_STRICT);
require_once 'PHPUnit/TextUI/TestRunner.php';
class ExampleTest extends PHPUnit_Framework_TestCase
{
public function testOne()
{
$this->assertTrue(FALSE);
}
}
$suite = new PHPUnit_Framework_TestSuite('ExampleTest');
PHPUnit_TextUI_TestRunner::run($suite);
编辑
刚才在你的问题中看到了Ubuntu 10.10。对于Ubuntu安装,我建议在终端中执行以下命令:
sudo pear uninstall phpunit/PHPUnit
sudo apt-get install phpunit
sudo /etc/init.d/apache2 restart
注意:如果您没有通过pear安装phpunit,请不要发出第一行。最后一行似乎是必需的(至少在我的情况下)。
sudo /etc/init.d/apache2 reload # Or
sudo service apache2 restart
phpunit MyClassTest
的东西? - netcoder