每次运行测试时,它都会在第一次运行时失败,在第二次运行时通过。
这是代码:
/** @test */
public function some_function_test()
{
$file = file_exists($this->path.'file');
if ($file) {
echo "\n file exists! \n";
}else{
$this->createFile;
}
$this->assertEquals($file, true);
}
当我删除文件并再次运行测试时,它失败了。 这告诉我断言在我的 if 语句之前运行。
如果断言首先运行,我可以让它等待我的条件测试吗?
最佳答案
您的断言将永远在if
之前运行。
您的测试失败,因为在 else
分支中,在使用 createFile
创建文件后,您没有更改 $file
,因此在 else
分支 $file
仍然是 false
。我想您需要将 $file
更改为 true
:
public function some_function_test()
{
$file = file_exists($this->path.'file');
if ($file) {
echo "\n file exists! \n";
}else{
$this->createFile(); // you're calling a method, aren't you?
$file = true;
}
$this->assertEquals($file, true);
// or simplier:
// $this->assertTrue($file);
}
关于php - 如何让PHPUnit等待条件测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49377355/