PHPUnit测试套件包含路径

use*_*931 12 php phpunit unit-testing

使用phpunit,我在使用包含路径时遇到了一些麻烦,不是为了phpunit本身,而是为了我的代码和测试目录.

我有以下代码结构:

Application
  -StringCalculator.php

tests
  -StringCalculatorTest.php
Run Code Online (Sandbox Code Playgroud)

在我的StringCalculatorTest.php里面我有一个require语句:

require_once('../StringCalculator.php');
Run Code Online (Sandbox Code Playgroud)

phpunit StringCalculatorTest.php从tests文件夹内部运行完美.

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

Application
  -StringCalculator.php

tests
  -StringCalculatorTest.php

phpunit.xml
Run Code Online (Sandbox Code Playgroud)

包含路径是拧紧的.我必须将require_once替换为

require_once('StringCalculator.php');
Run Code Online (Sandbox Code Playgroud)

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

sla*_*pon 11

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

<phpunit backupGlobals="true"
     backupStaticAttributes="false"
     bootstrap="bootstrap.php"
     cacheTokens="true"
     colors="true"
     ... and so on ...
</phpunit>
Run Code Online (Sandbox Code Playgroud)

然后在您的bootstrap文件中,您可以设置包含路径,包括重要文件等.

set_include_path(get_include_path() . PATH_SEPARATOR . '../my/sources');
Run Code Online (Sandbox Code Playgroud)

配置文件包含在PHPunit文档的附录C中.

编辑:链接已更新


sli*_*ier 8

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

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

<php>
  <includePath>/path/to/ur/project</includePath>
</php>
Run Code Online (Sandbox Code Playgroud)