PHPUnit 错误“无法打开流:没有这样的文件或目录"

2022-01-25 00:00:00 unit-testing php phpunit

我刚刚安装了 PHPUnit Testing,但由于某种原因它不会读取我的文件.我确定它在正确的路径中,但是为什么 PHPUnit 找不到呢?

I just installed PHPUnit Testing and for some reason it won't read my file. I'm sure it's in the correct path, but why PHPUnit can't find it?

这是我的示例代码:

functions.php

functions.php

<?php 

 function my_addition($arg1, $arg2){
        return $arg1 + $arg2;

 ?>

这是要测试的代码和文件:

Here is the code and the file to test:

functions_test.php

functions_test.php

<?php 


use PHPUnitFrameworkTestCase;




class functions_test extends TestCase {


public function testmy_addition() {
    include("./data/functions.php");
    $result = my_addition(1,1);
    $this->assertEquals(2, $result);

}


}


?>

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

What can I do to get it to work and make it pass?

推荐答案

你应该可以使用 __DIR__ 了.包含语句中的/data/functions.php"
(即前后两个下划线).

You should be able to use __DIR__ . "/data/functions.php" in your include statement
(that's two underscores before and after).

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

I did a quick test in my test environment and had no problems.

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

The __DIR__ magic constant gives you the full path to the test file you are running. From the PHP website:

文件的目录.如果在包含内使用,则目录返回包含的文件.这相当于目录名(__FILE__).此目录名称没有尾部斜杠除非是根目录.

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.

相关文章