简体   繁体   English

PHPUnit测试套件包含路径

[英]PHPUnit test suite include path

Using phpunit and I am having some trouble with include paths, not for phpunit itself, but for my code and tests directory. 使用phpunit,我在使用包含路径时遇到了一些麻烦,不是为了phpunit本身,而是为了我的代码和测试目录。

I have the following code structure: 我有以下代码结构:

Application
  -StringCalculator.php

tests
  -StringCalculatorTest.php

Inside my StringCalculatorTest.php i have a require statement: 在我的StringCalculatorTest.php里面我有一个require语句:

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

Running phpunit StringCalculatorTest.php from inside the tests folder works perfectly. 从tests文件夹中运行phpunit StringCalculatorTest.php非常有效。

However, when i then introduce a phpunit.xml configuration file in the root directory ie 但是,当我在根目录中引入phpunit.xml配置文件时,即

Application
  -StringCalculator.php

tests
  -StringCalculatorTest.php

phpunit.xml

the include path is screwed. 包含路径是拧紧的。 I have to replace the require_once to 我必须将require_once替换为

require_once('StringCalculator.php');

What is the correct way to set include paths between the application and the test directory? 在应用程序和测试目录之间设置包含路径的正确方法是什么?

The best place to set your PHP include path is in your bootstrap file. 设置PHP包含路径的最佳位置是在引导程序文件中。 Usually, your phpunit.xml file will include a bootstrap attribute: 通常,您的phpunit.xml文件将包含一个bootstrap属性:

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

Then in your bootstrap file you can set include paths, include important files, etc.. 然后在您的bootstrap文件中,您可以设置包含路径,包括重要文件等。

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

The config file is covered in Appendix C of the PHPunit docs. 配置文件包含在PHPunit文档的附录C中。

EDIT: Link updated 编辑:链接已更新

I know the question already been answer 我知道问题已经回答了
This answer is for future visitor 这个答案是为未来的访客

According to phpunit documentation, u can use includePath directive in your phpunit.xml for your inclusion path 根据phpunit文档,您可以在phpunit.xml使用includePath指令作为包含路径

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

First, I don't get how require_once('../StringCalculator.php'); 首先,我没有得到require_once('../StringCalculator.php'); works, it should rather be: require_once('../Application/StringCalculator.php'); 工作,它应该是: require_once('../Application/StringCalculator.php'); .

Then, slashingweapon answer is good and it's the best IMO, however if you don't want that much trouble, you can specify your require_once to start from the directory of the current file: 然后,slashingweapon答案是好的,它是最好的IMO,但是如果你不想那么麻烦,你可以指定你的require_once从当前文件的目录开始:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM