简体   繁体   English

PHPUnit 测试的目录布局?

[英]Directory layout for PHPUnit tests?

I'm a longtime Java programmer working on a PHP project, and I'm trying to get PHPUnit up and working.我是一名长期从事 PHP 项目的 Java 程序员,我正在尝试让 PHPUnit 正常工作。 When unit testing in Java, it's common to put test case classes and regular classes into separate directories, like this -在 Java 中进行单元测试时,通常会将测试用例类和常规类放在不同的目录中,如下所示 -

/src  
  MyClass.java

/test  
  MyClassTest.java

and so on.等等。

When unit testing with PHPUnit, is it common to follow the same directory structure, or is there a better way to lay out test classes?使用 PHPUnit 进行单元测试时,是否通常遵循相同的目录结构,或者有更好的方法来布置测试类? So far, the only way I can get the "include("MyClass.php")" statement to work correctly is to include the test class in the same directory, but I don't want to include the test classes when I push to production.到目前为止,让“include("MyClass.php")”语句正常工作的唯一方法是将测试 class 包含在同一目录中,但我不想在推送时包含测试类生产。

I think it's a good idea to keep your files separate.我认为将文件分开是个好主意。 I normally use a folder structure like this:我通常使用这样的文件夹结构:

/myapp/src/        <- my classes
/myapp/tests/       <- my tests for the classes
/myapp/public/      <- document root

In your case, for including the class in your test file, why not just pass the the whole path to the include method?在您的情况下,为了在您的测试文件中包含 class ,为什么不将整个路径传递给 include 方法?

include('/path/to/myapp/src/MyClass.php');

or或者

include('../src/MyClass.php');

You need to modify PHP's include_path so that it knows where to find MyClass.php when you include() it in your unit test.您需要修改 PHP 的 include_path 以便当您在单元测试中include()它时,它知道在哪里可以找到 MyClass.php。

You could have something like this at the top of your test file (preceding your include):你可以在你的测试文件的顶部有这样的东西(在你的包含之前):


set_include_path(get_include_path() . PATH_SEPARATOR . "../src");

This appends your src directory onto the include path and should allow you to keep your real code separate from your test code.这会将您的src目录附加到包含路径中,并且应该允许您将真实代码与测试代码分开。

Brian Phillips's answer does not go quite far enough, in my experience.根据我的经验,Brian Phillips 的回答远远不够 go 。 You don't know what the current directory is when your tests are run by PHPUnit.当您的测试由 PHPUnit 运行时,您不知道当前目录是什么。 So you need to reference the absolute path of the test class file in your set_include_path() expression.因此,您需要在 set_include_path() 表达式中引用测试 class 文件的绝对路径。 Like this:像这样:

set_include_path(get_include_path() . PATH_SEPARATOR . 
                        dirname(__FILE__) . "/../src");

This fragment can be placed in its own header file SetupIncludePath.php and included in test files with a 'require_once', so that test suites don't append the path multiple times.这个片段可以放在它自己的 header 文件 SetupIncludePath.php 并包含在带有“require_once”的测试文件中,这样测试套件就不会多次 Z9516DFB15F51C7EE19A4D46B8DZC0 路径。

I put my test cases next the the source in a file with the same name but a.phpt extension.我将我的测试用例放在源代码旁边的一个文件中,该文件具有相同的名称但扩展名为 .phpt。 The deployment script simply filters out *.phpt when they push to production.部署脚本在推送到生产环境时只是过滤掉 *.phpt。

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

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