【发布时间】:2016-09-21 23:24:02
【问题描述】:
我在 phpunit 测试中包含一个文件时遇到了一些问题。例如:当我在 PhpStorm 中执行以下代码时,我得到了预期的输出。
代码:
class NifvalidationTest extends PHPUnit_Framework_TestCase
{
public function test_apiRequest()
{
$result = 1+1;
$this->assertEquals(2, $result);
}
}
输出:
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
但是当我需要使用包含访问另一个类的方法时,我没有得到预期的输出。举个例子,当我执行以下代码时:
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
关于为什么包含会破坏测试的任何想法?
注意1:在上面的例子中我不需要包含文件,但我需要在另一个测试中。
注意2:文件'nifvalidation.php'的路径是正确的。
【问题讨论】:
-
感谢@FelippeDuarte 的回答,但我无法理解该问题的最佳答案。
-
您的问题似乎与您的 nifvalidation.php 文件有关。你能给我们看看代码吗?
-
@FelippeDuarte 你是对的。问题出在我的 nifvalidation.php 文件上,因为我使用不同的文件进行了测试并且没有问题。我的 nifvalidation.php 有一个扩展 prestashop 类的类: PS_VERSION')) { exit; } class Nifvalidation extends Module { //... } 问题在于类定义之前的 if 条件。但如果我没有这种情况,则找不到“模块”类。
标签: php testing include phpunit phpstorm