PHPUnit 错误 "Failed to open stream: No such file or directory"
PHPUnit error "Failed to open stream: No such file or directory"
我刚刚安装了 PHPUnit Testing,但出于某种原因它无法读取我的文件。我确定它在正确的路径中,但为什么 PHPUnit 找不到它?
这是我的示例代码:
functions.php
<?php
function my_addition($arg1, $arg2){
return $arg1 + $arg2;
?>
这是要测试的代码和文件:
functions_test.php
<?php
use PHPUnit\Framework\TestCase;
class functions_test extends TestCase {
public function testmy_addition() {
include("./data/functions.php");
$result = my_addition(1,1);
$this->assertEquals(2, $result);
}
}
?>
我该怎么做才能让它生效并通过?
您应该可以在包含语句中使用 __DIR__ . "/data/functions.php"
(也就是前后两个下划线)
我在我的测试环境中做了一个快速测试,没有任何问题。
__DIR__
魔法常量为您提供了 运行 测试文件的完整路径。来自 PHP 网站:
The directory of the file. If used inside an include, the directory of
the included file is returned. This is equivalent to
dirname(__FILE__)
. This directory name does not have a trailing slash
unless it is the root directory.
我刚刚安装了 PHPUnit Testing,但出于某种原因它无法读取我的文件。我确定它在正确的路径中,但为什么 PHPUnit 找不到它?
这是我的示例代码:
functions.php
<?php
function my_addition($arg1, $arg2){
return $arg1 + $arg2;
?>
这是要测试的代码和文件:
functions_test.php
<?php
use PHPUnit\Framework\TestCase;
class functions_test extends TestCase {
public function testmy_addition() {
include("./data/functions.php");
$result = my_addition(1,1);
$this->assertEquals(2, $result);
}
}
?>
我该怎么做才能让它生效并通过?
您应该可以在包含语句中使用 __DIR__ . "/data/functions.php"
(也就是前后两个下划线)
我在我的测试环境中做了一个快速测试,没有任何问题。
__DIR__
魔法常量为您提供了 运行 测试文件的完整路径。来自 PHP 网站:
The directory of the file. If used inside an include, the directory of the included file is returned. This is equivalent to
dirname(__FILE__)
. This directory name does not have a trailing slash unless it is the root directory.