📜  PHPUnit | assertDirectoryExists()函数

📅  最后修改于: 2022-05-13 01:56:38.531000             🧑  作者: Mango

PHPUnit | assertDirectoryExists()函数

assertDirectoryExists()函数是 PHPUnit 中的内置函数,用于断言目录路径是否存在。如果给定的目录路径存在,则此断言将返回 true,否则返回 false。如果为真,则断言的测试用例通过,否则测试用例失败。

句法:

assertDirectoryExists( integer $directory, string $message = '' )

参数:此函数接受两个参数,如上述语法所示。参数说明如下:

  • $directory:此参数是一个字符串,表示目录路径。
  • $message:此参数采用字符串值。当测试用例失败时,此字符串消息显示为错误消息。

下面的程序说明了 PHPUnit 中的 assertDirectoryExists()函数:

方案一:

assertDirectoryExists(
            $directoryPath,
            "directoryPath doesn't exists"
        );
    }
}
  
?>

输出:

PHPUnit 8.2.5 by Sebastian Bergmann and contributors.

F                                                                   1 / 1 (100%)

Time: 68 ms, Memory: 10.00 MB

There was 1 failure:

1) GeeksPhpunitTestCase::testNegativeTestcaseForAssertDirectoryExists
directoryPath doesn't exists
Failed asserting that directory "/home/shivam/Documents/geeksDoesNotExist/" exists.

/home/shivam/Documents/geeks/phpunit/abc.php:13

FAILURES!
Tests: 1, Assertions: 1, Failures: 1.

方案二:

assertDirectoryExists(
            $directoryPath,
            "directoryPath exists"
        );
    }
}
  
?>

输出:

PHPUnit 8.2.5 by Sebastian Bergmann and contributors.

.                                                                   1 / 1 (100%)

Time: 67 ms, Memory: 10.00 MB

OK (1 test, 1 assertion)

注意:要使用 phpunit 运行测试用例,请按照此处的步骤操作。此外,phpunit 7 及更高版本支持 assertDirectoryExists()。