简体   繁体   English

PHPUnit测试应在失败时通过

[英]PHPUnit test passes when it should fail

I am writing a PHPUnit test suite and have run into a bit of a problem. 我正在编写一个PHPUnit测试套件,但遇到了一些问题。

Here is the test: 这是测试:

public function testSomething(){
  $stub = $this->getMockForAbstractClass('\core\classes\ImportedFile');

  $stub->expects($this->exactly(4))
       ->method('thismethoddoesntexist');

  $this->markTestIncomplete('not finished implementing');
}

For some reason this test is not failing. 由于某种原因,该测试不会失败。 It should because the method does not exist and is therefore not called even once, let alone 4 times. 应该因为该方法不存在,所以甚至不会调用一次,更不用说调用4次了。 It doesn't matter what I put in there, even if I put in a method name that does exist and say that I'm expecting it to run 100,000 times it still passes when this is obviously wrong. 即使我输入了确实存在的方法名称并说我希望它运行100,000次,即使这显然是错误的,它仍然可以通过,即使输入什么也没关系。

I find this very strange since I have similar checks in previous tests which work properly. 我发现这很奇怪,因为我在以前的测试中也进行过类似的检查,这些检查可以正常工作。

Has anyone else experienced problems like this? 还有其他人遇到过这样的问题吗?

markTestIncomplete throw special exception, witch end the test. markTestIncomplete抛出特殊异常,巫婆结束测试。 Checks for 'expects' in mocks are skipped. 模拟中对“期望”的检查将被跳过。

public static function markTestIncomplete($message = '')
    {
        throw new PHPUnit_Framework_IncompleteTestError($message);
    }

Found the cause of the problem. 找到了问题的原因。 It seems to be caused by the call to $this->markTestIncomplete() . 这似乎是由于调用$this->markTestIncomplete() For some reason having this in the test causes it not to fail, at least in this case, even when it should. 由于某种原因,至少在这种情况下,即使在应该的情况下,在测试中进行测试也不会导致失败。

Removing the call to markTestIncomplete makes it behave normally and fail when it should. 删除对markTestIncomplete的调用会使它正常运行,并在应有的情况下失败。 I tend to leave the markTestIncomplete method in a test until it is 100% complete, and assume that if I run the test, even in its incomplete state, it should fail if an expectation is not met. 我倾向于将markTestIncomplete方法留在测试中,直到100%完成为止,并假设如果我运行测试(即使处于未完成状态),则如果未达到期望,它也会失败。 Is this not a fair assumption to make? 这不是一个公平的假设吗? Is this a bug in PHPUnit? 这是PHPUnit中的错误吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM