PHPUnit错误“打开流失败:没有这样的文件或目录”


PHPUnit error "Failed to open stream: No such file or directory"

我刚刚安装了PHPUnit Testing,由于某种原因它不能读取我的文件。我确信它在正确的路径上,但为什么PHPUnit找不到它?

下面是我的示例代码:

显然也
<?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);
}

}

?>

我该怎么做才能让它工作并通过呢?

您应该能够在include语句中使用__DIR__ . "/data/functions.php"
(即前后两个下划线)。

我在我的测试环境中做了一个快速测试,没有问题。

__DIR__魔术常数为您提供了您正在运行的测试文件的完整路径。来自PHP网站:

文件所在的目录。如果在include中使用,则返回包含的文件。这个等价于dirname(__FILE__)。此目录名没有尾斜杠除非是根目录