Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHPUnit测试的目录布局?_Php_Unit Testing_Phpunit - Fatal编程技术网

PHPUnit测试的目录布局?

PHPUnit测试的目录布局?,php,unit-testing,phpunit,Php,Unit Testing,Phpunit,我是一名长期从事PHP项目的Java程序员,我正在尝试让PHPUnit启动并工作。在Java中进行单元测试时,通常会将测试用例类和常规类放在单独的目录中,如下所示- /src MyClass.java /test MyClassTest.java 等等 当使用PHPUnit进行单元测试时,遵循相同的目录结构是常见的,还是有更好的方法来布置测试类?到目前为止,让“include”(“MyClass.php”)语句正常工作的唯一方法是将测试类包含在同一目录中,但我不想在投入生产时

我是一名长期从事PHP项目的Java程序员,我正在尝试让PHPUnit启动并工作。在Java中进行单元测试时,通常会将测试用例类和常规类放在单独的目录中,如下所示-

/src  
  MyClass.java

/test  
  MyClassTest.java
等等


当使用PHPUnit进行单元测试时,遵循相同的目录结构是常见的,还是有更好的方法来布置测试类?到目前为止,让“include”(“MyClass.php”)语句正常工作的唯一方法是将测试类包含在同一目录中,但我不想在投入生产时包含测试类。

您需要修改php的include\u路径,以便在
include()时它知道在哪里可以找到MyClass.php
在单元测试中使用它

您可以在测试文件的顶部(在include之前)有类似的内容:


这会将
src
目录附加到include路径上,并允许您将真实代码与测试代码分开。

我认为将文件分开是个好主意。我通常使用如下文件夹结构:

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


我将测试用例放在源代码旁边的一个文件中,文件名相同,但扩展名为.phpt。部署脚本在投入生产时只是过滤掉*.phpt。

根据我的经验,Brian Phillips的回答还不够深入。当您的测试由PHPUnit运行时,您不知道当前目录是什么。因此,您需要在set_include_path()表达式中引用测试类文件的绝对路径。像这样:

set_include_path(get_include_path() . PATH_SEPARATOR . 
                        dirname(__FILE__) . "/../src");
此片段可以放在自己的头文件SetupIncludePath.php中,并包含在带有“require_once”的测试文件中,这样测试套件就不会多次附加路径。

交叉引用:
include('/path/to/myapp/src/MyClass.php');
include('../src/MyClass.php');
set_include_path(get_include_path() . PATH_SEPARATOR . 
                        dirname(__FILE__) . "/../src");