让PHPUnit工作 - 包含路径设置不正确?

Jar*_*les 21 php pear phpunit unit-testing

我正在尝试让PHPUnit在我的开发环境中运行,但是当我在脚本中包含PHPUnit时,我遇到了一些障碍.我知道我需要在PHP上设置include路径,但是我试过的每个组合都会失败,而编译器看不到PHPUnit_Framework_TestCase类.

我刚刚在PHP和PEAR上运行了更新,并且计算机上安装了PHPUnit,因为我可以通过命令行访问它.

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

梨在/ usr/share/php/PEAR

有什么我想念的吗?这是我第一次尝试使用PHPUnit甚至是PEAR的东西.我在Ubuntu 10.10上.任何帮助,将不胜感激.

编辑 - 我的PHP ini中的include路径中没有任何内容.现在代码就是这样

<?php
class Stacktest extends PHPUnit_Framework_TestCase
{

}
Run Code Online (Sandbox Code Playgroud)

我不知道要在include路径中包含什么或设置什么,因为对于关于PHPUnit的网络上的所有信息来说,这些一点点的信息都是严重缺失的.

xrs*_*stf 26

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

require 'PHPUnit/Autoload.php'; // PEAR should be in your include_path
Run Code Online (Sandbox Code Playgroud)

  • 哪里?在每个测试文件的顶部? (6认同)

Poe*_*rin 17

如果您正确安装了phpunit(通过PEAR),那么就不需要包含文件了; 你只需要改变你用它来测试php文件的方式(例如你用它来测试文件是否通过浏览器类型localhost来工作).使用phpunit,您可以使用命令行; 第5章使用命令行给出了相同的例子(我认为它是一个标准).所以,如果你正确安装它,你可以这样做:

  1. 文件ExampleTest.php,位于localhost的根目录(对我来说这是/ var/www):

    class ExampleTest extends PHPUnit_Framework_TestCase
    {
        public function testOne()
        {
            $this->assertTrue(FALSE);
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  2. 打开控制台(Mac或Linux上的终端,Win上的命令提示符),导航到localhost文档根目录(保存ExampleTest.php的位置)并键入以下内容:

    phpunit --verbose ExampleTest.php
    
    Run Code Online (Sandbox Code Playgroud)
  3. 你应该看到:

    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.
    
    Run Code Online (Sandbox Code Playgroud)

注意:以上所有假设您正确安装了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);
Run Code Online (Sandbox Code Playgroud)

编辑

刚刚在您的问题中发现了Ubuntu 10.10.对于Ubuntu安装我推荐这个:在终端中执行:

sudo pear uninstall phpunit/PHPUnit
sudo apt-get install phpunit
sudo /etc/init.d/apache2 restart
Run Code Online (Sandbox Code Playgroud)

注意:如果您没有通过pear安装phpunit,请不要发出第一行.似乎需要最后一行(至少在我的情况下).

sudo /etc/init.d/apache2 reload # Or
sudo service apache2 restart
Run Code Online (Sandbox Code Playgroud)


Jef*_*vis 6

将此行添加到您的php.ini:

include_path=".:/usr/share/php:/usr/share/pear:/usr/share/php/PHPunit:/usr/share/php/PEAR"
Run Code Online (Sandbox Code Playgroud)

保存文件并重新启动apache.

另外,请确保编辑正确php.ini.尝试使用它locate php.ini来查找它可能隐藏的所有地方.它通常位于/usr某个目录下.