PHPUnit测试套件包含路径 [英] PHPUnit test suite include path

查看:69
本文介绍了PHPUnit测试套件包含路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Using phpunit and I am having some trouble with include paths, not for phpunit itself, but for my code and tests directory.

我具有以下代码结构:

Application
  -StringCalculator.php

tests
  -StringCalculatorTest.php

在我的StringCalculatorTest.php内,我有一条require语句:

Inside my StringCalculatorTest.php i have a require statement:

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

从tests文件夹中运行phpunit StringCalculatorTest.php效果很好.

Running phpunit StringCalculatorTest.php from inside the tests folder works perfectly.

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

However, when i then introduce a phpunit.xml configuration file in the root directory i.e.

Application
  -StringCalculator.php

tests
  -StringCalculatorTest.php

phpunit.xml

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

the include path is screwed. I have to replace the require_once to

require_once('StringCalculator.php');

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

What is the correct way to set include paths between the application and the test directory?

推荐答案

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

The best place to set your PHP include path is in your bootstrap file. Usually, your phpunit.xml file will include a bootstrap attribute:

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

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

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

The config file is covered in Appendix C of the PHPunit docs.

编辑:链接已更新

这篇关于PHPUnit测试套件包含路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆