Mic*_*kes 5 php phpunit unit-testing
我刚刚安装了 PHPUnit 测试,由于某种原因它不会读取我的文件。我确定它在正确的路径中,但为什么 PHPUnit 找不到它?
这是我的示例代码:
函数.php
<?php
function my_addition($arg1, $arg2){
return $arg1 + $arg2;
?>
Run Code Online (Sandbox Code Playgroud)
这是要测试的代码和文件:
函数_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);
}
}
?>
Run Code Online (Sandbox Code Playgroud)
我该怎么做才能让它发挥作用并使其通过?
您应该可以__DIR__ . "/data/functions.php"在 include 语句中使用
(前后两个下划线)。
我在我的测试环境中做了一个快速测试,没有任何问题。
该__DIR__魔术不断给你的完整路径正在运行的测试文件。来自 PHP 网站:
文件的目录。如果在包含中使用,则返回包含文件的目录。这相当于
dirname(__FILE__). 除非它是根目录,否则此目录名称没有尾部斜杠。