如何在phpunit测试中包含文件?
我在phpunit测试中包含文件时遇到一些问题。例如:当我在PhpStorm中执行以下代码时,我得到了预期的输出 代码: 输出:如何在phpunit测试中包含文件?,php,testing,include,phpunit,phpstorm,Php,Testing,Include,Phpunit,Phpstorm,我在phpunit测试中包含文件时遇到一些问题。例如:当我在PhpStorm中执行以下代码时,我得到了预期的输出 代码: 输出: Testing started at 16:58 ... PHPUnit 5.2.12 by Sebastian Bergmann and contributors. Time: 120 ms, Memory: 11.50Mb OK (1 test, 1 assertion) Process finished with exit code 0 但是当我需要
Testing started at 16:58 ...
PHPUnit 5.2.12 by Sebastian Bergmann and contributors.
Time: 120 ms, Memory: 11.50Mb
OK (1 test, 1 assertion)
Process finished with exit code 0
但是当我需要使用include从另一个类访问一个方法时,我得不到预期的输出。例如,当我执行以下代码时:
class NifvalidationTest extends PHPUnit_Framework_TestCase
{
public function test_apiRequest()
{
include('/../nifvalidation.php');
$result = 1+1;
$this->assertEquals(2, $result);
}
}
我得到的是这个,而不是预期的输出:
Testing started at 17:05 ...
PHPUnit 5.2.12 by Sebastian Bergmann and contributors.
Process finished with exit code 0
你知道为什么include会打破测试吗
注1:在上面的例子中,我不需要包含该文件,但我需要在另一个测试中
注2:文件“niffalization.php”的路径是正确的。我认为您的include路径是错误的。 您的结构可能有点像这样
ParentDir
->niffalization.php
->testsFolder
->NifvalidationTest.php
而不是
include('/../nifvalidation.php')
使用
使用require()
或require_once()
而不是使用include()
从命令行调用测试时,可以使用来包含任何文件、定义常量和加载所有变量、类等
对于更自动化的方法,您还可以选择包含引导信息的:
/测试文件夹
在本例中,根据您对niffalization.php
内容的评论,脚本将退出,因为未定义PS\u VERSION
。如果您正在执行只需要定义一个虚拟常量的独立单元测试,那么您可以定义该常量以显示在您的测试环境中。然后,您可以简单地引导niffalization.php
文件
/测试文件夹
谢谢您的回答@FelippeDuarte,但是我在理解这个问题的最佳答案时遇到了一些困难。您的问题似乎与您的nifvalidation.php文件有关。你能给我们看看密码吗?@FelippeDuarte你说得对。问题在于我的niffalization.php文件,因为我使用不同的文件进行了测试,没有问题。My nifvalidation.php有一个扩展prestashop类的类:是的,结构如下。我试着按照你的建议使用include,但不幸的是也不起作用。
include('/../nifvalidation.php')
include(dirname(__FILE__)."/../nifvalidation.php");
--bootstrap <file> A "bootstrap" PHP file that is run before the tests.
phpunit --bootstrap autoload.php testsFolder/NifvalidationTest