你如何在 PHPUnit 中重复测试?

vel*_*lo9 3 php phpunit

我知道“--repeat”选项,但我宁愿在测试和每个测试中定义重复。在我的单元测试中,有些测试我不想重复,有些测试我比其他测试更想重复。

我刚在想:

protected function tearDown() {
  if (test has not been ran 3 times) {
      $this->runTest(); // Re-run the test
  }
}
Run Code Online (Sandbox Code Playgroud)

这似乎不起作用,$this->run() 也不起作用。我看过 PHPUnit 源代码,但我不确定。我猜它正在检查测试状态,如果它已经运行,它会拒绝再次运行它。

reu*_*sen 7

phpunit 在命令行运行器中有一个重复选项。对于重复 3 次的测试,其工作原理如下:

phpunit --repeat 3 myTest.php


Lou*_*pax 6

这是一种有点迂回的方法,但这是我能想到的最干净的方法:

/**
 * @dataProvider numberOfTests
 */
public function test()
{
    // Do your test
}

public function numberOfTests() 
{
    for ($i = 0; $i < 100; $i++) {
       yield [];
    }
}
Run Code Online (Sandbox Code Playgroud)

这样做的好处是每次调用循环时都会运行 setUp 和tearDown 方法。


ncr*_*ins -1

这不能通过 do-while 循环来实现吗?

protected function tearDown() {
    $i = 0;
    do {
        $this->runTest(); // Re-run the test
        $i++;
    } while($i < 3);
}
Run Code Online (Sandbox Code Playgroud)