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

查看:97
本文介绍了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 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);

}


}


?>

我该怎么办才能使其通过并通过?

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:

文件目录.如果在include中使用,则目录 返回包含的文件.这相当于 dirname(__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天全站免登陆