duansha6410 2010-11-03 21:30
浏览 74
已采纳

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

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.

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 is installed at /usr/share/php/PHPunit

Pear is at /usr/share/php/PEAR

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.

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
{

}

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.

  • 写回答

5条回答 默认 最新

  • duanchifo2866 2011-01-06 10:08
    关注

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

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

    Edit

    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
    

    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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?