代码之家  ›  专栏  ›  技术社区  ›  Jarrod Nettles

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

  •  20
  • Jarrod Nettles  · 技术社区  · 15 年前

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

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

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

    梨在

    我有什么遗漏吗?这是我第一次尝试使用PHPUnit,甚至是梨中的一些东西。我在Ubuntu 10.10上。任何帮助都将不胜感激。

    编辑-我的PHP ini的include路径中没有任何内容。现在密码是

    <?php
    class Stacktest extends PHPUnit_Framework_TestCase
    {
    
    }
    

    5 回复  |  直到 15 年前
        1
  •  17
  •   elixenide Ren    12 年前

    如果您正确安装了phpunit(通过PEAR),那么就不需要包含文件;您只需要改变使用它测试php文件的方式(例如,通过转到浏览器类型localhost使用它来测试文件是否工作)。使用phpunit,您可以使用命令行; chapter 5 给出了使用命令行的相同示例(我假设它是标准的)。因此,如果安装正确,可以执行以下操作:

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

      class ExampleTest extends PHPUnit_Framework_TestCase
      {
          public function testOne()
          {
              $this->assertTrue(FALSE);
          }
      }
      
    2. phpunit --verbose ExampleTest.php
      
    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.
      

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

    编辑

    刚刚在你的问题中发现了Ubuntu10.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
    
        2
  •  26
  •   xrstf    15 年前

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

    require 'PHPUnit/Autoload.php'; // PEAR should be in your include_path
    
        3
  •  6
  •   prayagupa soxunyi    13 年前

    将此行添加到 php.ini :

    include_path=".:/usr/share/php:/usr/share/pear:/usr/share/php/PHPunit:/usr/share/php/PEAR"
    

    另外,请确保编辑正确的 php.ini文件 locate php.ini /usr 某个地方的目录。

        4
  •  2
  •   Travis Webb    15 年前

    确保每次使用include()或require()时,都在实际文件名前面加上 dirname(__FILE__) . 这将确保包含的文件位于相对于包含的实际文件指定的路径。通过deftault,PHP包含相对于被调用以启动程序的文件的内容。

        5
  •  0
  •   JTHouseCat    12 年前

    通过在脚本顶部使用set_include_path,我可以使它正常工作(参见下面的示例 http://www.siteconsortium.com/h/p1.php?id=php002 ),然后我还必须包括,因为我正在运行硒测试,但它工作。

    require_once ('PHPUnit/Autoload.php');
    require_once 'PHPUnit/Extensions/SeleniumTestCase.php';
    
    推荐文章