【问题标题】:PHPUnit test suite include pathPHPUnit 测试套件包含路径
【发布时间】:2012-10-19 06:16:12
【问题描述】:

使用 phpunit,但我在包含路径方面遇到了一些问题,不是针对 phpunit 本身,而是针对我的代码和测试目录。

我的代码结构如下:

Application
  -StringCalculator.php

tests
  -StringCalculatorTest.php

在我的 StringCalculatorTest.php 中,我有一个 require 语句:

require_once('../StringCalculator.php');

在测试文件夹中运行phpunit StringCalculatorTest.php 效果很好。

但是,当我随后在根目录中引入一个 phpunit.xml 配置文件时,即

Application
  -StringCalculator.php

tests
  -StringCalculatorTest.php

phpunit.xml

包含路径被搞砸了。我必须将 require_once 替换为

require_once('StringCalculator.php');

在应用程序和测试目录之间设置包含路径的正确方法是什么?

【问题讨论】:

    标签: php unit-testing phpunit


    【解决方案1】:

    设置 PHP 包含路径的最佳位置是在引导文件中。通常,您的 phpunit.xml 文件将包含引导属性:

    <phpunit backupGlobals="true"
         backupStaticAttributes="false"
         bootstrap="bootstrap.php"
         cacheTokens="true"
         colors="true"
         ... and so on ...
    </phpunit>
    

    然后在您的引导文件中,您可以设置包含路径、包含重要文件等。

    set_include_path(get_include_path() . PATH_SEPARATOR . '../my/sources');
    

    配置文件包含在 PHPunit 文档的Appendix C 中。

    编辑:链接已更新

    【讨论】:

      【解决方案2】:

      我知道这个问题已经回答了
      这个答案是给未来的访客的

      根据 phpunit 文档,您可以在 phpunit.xml 中使用 includePath 指令作为包含路径

      <php>
        <includePath>/path/to/ur/project</includePath>
      </php>
      

      【讨论】:

        【解决方案3】:

        首先,我不明白require_once('../StringCalculator.php'); 的工作原理,它应该是:require_once('../Application/StringCalculator.php');

        那么,slashingweapon 的答案很好,它是最好的 IMO,但是如果你不想那么麻烦,你可以指定你的 require_once 从当前文件的目录开始:

        require_once(__DIR__ . '../Application/StringCalculator.php');
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2016-03-09
          • 2016-09-21
          • 2014-09-21
          • 2014-12-13
          • 2013-08-21
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多