如何使PHPUnit工作 - 包含路径未正确设置?

21

我正在尝试在开发环境中使用PHPUnit,但是当涉及到在我的脚本中包含PHPUnit时,我遇到了一些障碍。我知道我需要在PHP上设置包含路径,但是我尝试的每种组合都失败了,编译器看不到PHPUnit_Framework_TestCase类。

我刚刚更新了PHP和PEAR并安装了PHPUnit,因为我可以通过命令行正常访问它。

PHPUnit安装在/usr/share/php/PHPunit

Pear安装在/usr/share/php/PEAR

我有什么遗漏吗?这是我第一次尝试使用PHPUnit,甚至是PEAR中的任何内容。我使用的是Ubuntu 10.10。感谢任何帮助。

编辑- 我的PHP ini中没有包含路径。现在代码只是:

<?php
class Stacktest extends PHPUnit_Framework_TestCase
{

}

我不知道在包含路径中应该包含什么或设置什么,因为似乎在网络上有关PHPUnit的所有信息都严重缺乏这一点重要信息。


你能提供更多关于如何在你的脚本中使用PHPUnit的信息吗? - Tim Fountain
1
你正在尝试如何运行测试用例?你是否在命令行使用类似于 phpunit MyClassTest 的东西? - netcoder
1
你是说你无法包含TestCase.php吗?请通过执行echo get_include_path()来查找您的包含路径。 - Cobby
执行 echo get_include_path() 会输出以下结果:.:/usr/share/php:/usr/share/pear - Jarrod Nettles
5个回答

26

从PHPUnit 3.5开始,您需要自己包含自动加载器:

require 'PHPUnit/Autoload.php'; // PEAR should be in your include_path

这是正确的解决方案 - 自动加载器必须显式地包含。仍然需要确保您将包含路径设置为pear以及phpunit。 - Jarrod Nettles
6
在哪里?在每个测试文件的顶部吗? - B Seven
@BSeven,我不确定如何帮助你,但这个答案足以让我在我的情况下开始。我正在使用PhpStorm 6,我能够进入首选项(项目)> PHP > PHPUnit,并将PHPUnit库设置为“使用自定义加载器”,然后我将路径设置为我的项目的vendor/autoload.php文件。 - Tyler Collier

17
如果你正确安装了phpunit(通过PEAR),那么就不需要包含文件;你只需要改变使用它测试php文件的方式(例如,你可以使用它来测试文件是否能够在浏览器中正常运行,输入localhost即可)。使用phpunit,你可以使用命令行;第5章提供了使用命令行的相同示例(我认为这是一个标准)。因此,如果你已经正确安装了phpunit,你可以这样做:
  1. 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);
        }
    }
    
  2. 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
    
  3. 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。
如果您确实希望在浏览器中运行测试,请使用以下代码。
# error reporting
ini_set('display_errors',1);
error_reporting(E_ALL|E_STRICT);

# include TestRunner
require_once 'PHPUnit/TextUI/TestRunner.php';

# our test class
class ExampleTest extends PHPUnit_Framework_TestCase
{
    public function testOne()
    {
        $this->assertTrue(FALSE);
    }
}

# run the test
$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

1
尝试在浏览器中运行时,它可以正确加载TestRunner.php文件,但无法加载父类PHPUnit_Runner_BaseTestRunner的文件。当我在浏览器中运行时,会出现以下错误:致命错误:在/usr/share/php/PHPUnit/TextUI/TestRunner.php的第60行找不到类'PHPUnit_Runner_BaseTestRunner'。 - Jarrod Nettles
1
在花费了数小时的时间尝试安装PHPUnit后,这个方法对我有效。 - B Seven
这是两年后的事了,@PoelincaDorin,但是phpunit的ubuntu软件包导致了对未定义方法PHP_CodeCoverage_Filter::getInstance()的调用。 - Owen Beresford
@OwenBeresford 你使用的是哪个版本的Ubuntu和PHP?虽然我已经很久没有玩过了,但如果你想的话,我可以试一试。 - Poelinca Dorin
1
@PoelincaDorin,PHP 5.4.17,PHPUnit 3.7.28,ubuntu 12.04 ~ 我很快就会对其进行评分。自从我写了这个之后,我通过dpkg将其删除,并破解了pear配置,然后通过pear添加它来解决问题。无论如何,还是谢谢。 - Owen Beresford
显示剩余2条评论

6
将以下行添加到您的php.ini文件中:
include_path=".:/usr/share/php:/usr/share/pear:/usr/share/php/PHPunit:/usr/share/php/PEAR"

保存文件并重新启动apache。

同时,确保您编辑正确的php.ini文件。尝试使用locate php.ini命令查找可能隐藏的所有位置。通常它位于/usr目录下的某个地方。


1
无需“查找”正确的文件。使用“php -i”获取完整配置,其中包含当前加载的配置文件路径。或在php文件中使用phpinfo()。 - Michiel van der Blonk
php -i | grep php.ini grep 是你的好朋友,但是如果你使用 Apache 中的 PHP,则应该通过添加 php_info() 并查找 "php.ini" 来从浏览器中检查。Ubuntu 文件位于 /etc/apache2/ 下,并且在 /etc/php5/ {apache2|cli|cgi} 下有更多文件。 - m3nda

2

每次使用 include() 或 require() 时,请确保在实际文件名前加上 dirname(__FILE__)。这样可以确保您包含的文件位于您指定的路径相对于包含它的实际文件。默认情况下,PHP 包含相对于调用程序启动的文件。


0

我通过在脚本顶部使用set_include_path(请参见此处的示例http://www.siteconsortium.com/h/p1.php?id=php002)使其工作,并且由于我运行了Selenium测试,所以还必须包含它,但是它起作用了。

require_once ('PHPUnit/Autoload.php');
require_once 'PHPUnit/Extensions/SeleniumTestCase.php';

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接