0

I am writing a PHPUnit test suite and have run into a bit of a problem.

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. 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.

I find this very strange since I have similar checks in previous tests which work properly.

Has anyone else experienced problems like this?

5
  • 1
    I don't see that you're actually calling any methods or assert anything... you're setting up expectations, but your test doesn't do anything.
    – Kryten
    Commented Jul 24, 2014 at 14:19
  • Yes, the test is only partway through being written - I will obviously add some method calls etc. to this. I still think it should be a test failure though, since I'm expecting a method to be called and it isn't. Commented Jul 24, 2014 at 14:29
  • markTestIncomplete is designed to prevent your test from failing. It isn't passing but your test suite will say that all the tests passed. phpunit.de/manual/current/en/…
    – Schleis
    Commented Jul 24, 2014 at 14:56
  • @Schleis I think there is some inconsistency though - if I create a test that consists only of $this->assertTrue(false) and then a call to markTestIncomplete, PHPUnit rightly fails the test. So why doesn't it do that in my example above? Commented Jul 24, 2014 at 15:12
  • Because the failed assertion is marked by an exception that prevents the call to markTestIncomplete from ever being called. In your case, the checks for mocks happen after the test has been completed. So markTestIncomplete is called and the test is marked as Incomplete.
    – Schleis
    Commented Jul 25, 2014 at 12:58

2 Answers 2

2

markTestIncomplete throw special exception, witch end the test. Checks for 'expects' in mocks are skipped.

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

Found the cause of the problem. It seems to be caused by the call to $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. 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. Is this not a fair assumption to make? Is this a bug in PHPUnit?

2
  • I use Mockery for my testing, so my experience is a little different, but Mockery requires a call to Mockery::close() after the test is run to properly resolve method call expectations. Perhaps something similar is required?
    – Kryten
    Commented Jul 24, 2014 at 14:34
  • Thanks for your comment - I'm not aware of anything like that in PHPUnit. I'll do a bit of googling. Commented Jul 24, 2014 at 15:14

Not the answer you're looking for? Browse other questions tagged or ask your own question.