PHPUnit 错误“无法打开流:没有这样的文件或目录" [英] PHPUnit error "Failed to open stream: No such file or directory"

查看:28
本文介绍了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.

这篇关于PHPUnit 错误“无法打开流:没有这样的文件或目录"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆