使PHPUnit工作-包含路径设置不正确? [英] Getting PHPUnit Working - Include Path not set correctly?

查看:101
本文介绍了使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

梨位于/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的include路径中没有任何内容.现在的代码就是

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
{

}

我不知道要包含什么或在include路径中设置什么,因为似乎对于Web上有关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,您可以使用命令行. 第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工作-包含路径设置不正确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆