沃梦达 / 编程问答 / php问题 / 正文

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

Getting PHPUnit Working - Include Path not set correctly?(让 PHPUnit 工作 - 包含路径设置不正确?)

本文介绍了让 PHPUnit 工作 - 包含路径设置不正确?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to get PHPUnit working on my development environment but I've hit a bit of a roadblock when it comes to including PHPUnit in my scripts. I know that I need to set the include path on PHP but every combination I've tried fails without the compiler seeing the PHPUnit_Framework_TestCase class.

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

I just ran updates on PHP and PEAR and PHPUnit is installed on the computer because I can access it through the command line just fine.

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

PHPUnit is installed at /usr/share/php/PHPunit

Pear 位于 /usr/share/php/PEAR

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

Is there something I'm missing? This is my first time trying to use PHPUnit or even something from PEAR for that matter. I'm on Ubuntu 10.10. Any help would be appreciated.

编辑 - 我的 PHP ini 的包含路径中没有任何内容.现在代码只是

Edit - There is nothing in the include path in my PHP ini. Right now the code is just

<?php
class Stacktest extends PHPUnit_Framework_TestCase
{

}

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

I have no idea what to include or what to set in the include path because it seems that for all the info on the web about PHPUnit, this little bit of information is critically absent.

推荐答案

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

If you installed phpunit correctly (through PEAR), then there is no need for an include file; you just need to change the way you use it to test php files (e.g. you use it to test if the file worked by going to the browser type localhost). With phpunit you can use the command line; chapter 5 gives the same example using the command line (I would assume it's a standard). So if you got it installed correctly you can do this:

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

  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);
    }
}

  • 打开一个控制台(Mac 或 Linux 上的终端,Win 上的命令提示符),导航到您的 localhost 文档根目录(您保存 ExampleTest.php 的位置)并输入以下内容:

  • 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.

    Notes: All the above assumes you installed phpunit correctly (as stated in chapter 3) and you restarted apache after that .

    如果您真的想在浏览器中运行测试,请使用以下代码

    if you really want to run tests in your browser use the following code

    # 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 安装,我建议这样做:在终端中执行:

    Just spotted Ubuntu 10.10 in your question. For Ubuntu install I recommend this: in terminal do:

    sudo pear uninstall phpunit/PHPUnit
    sudo apt-get install phpunit
    sudo /etc/init.d/apache2 restart
    

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

    Note: Don't issue the first line if you didn't install phpunit through pear. The last line seems to be needed (in my case at least).

    sudo /etc/init.d/apache2 reload # Or
    sudo service apache2 restart
    

    这篇关于让 PHPUnit 工作 - 包含路径设置不正确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

    本文标题为:让 PHPUnit 工作 - 包含路径设置不正确?

    基础教程推荐