views:

73

answers:

2

Is it possible to mark a test as "expected to fail" with PHPUnit? This would be useful when performing TDD, and you want to distinguish between genuinely failed tests, and tests that happen to fail because the associated code hasn't been written yet.

+7  A: 

I think in these cases, it's fairly standard to simply mark the test as skipped. Your tests will still run and the suite will pass, but the test runner will alert you of the skipped tests.

http://www.phpunit.de/manual/3.4/en/incomplete-and-skipped-tests.html

Ryan Chouinard
I guess that'll do. I was looking for something like the support for TODO blocks in Perl's Test::More module http://perldoc.perl.org/Test/More.html#Conditional-tests which will run the tests that are expected to fail, and report if they (unexpectedly) succeeded. (PHPUnit seems to only be able to skip completely.)
mjs
A: 

If you want to have a test fail but know that its failure was expected, you can add a message to the assertion that will output in the results:

public function testExpectedToFail()
{    
    $this->assertTrue(FALSE, 'I knew this would happen!');
}

In the results:

There was 1 failure:

1) testExpectedToFail(ClassTest)
I knew this would happen!
Clay Hinson
This would cause the suite to fail, which doesn't seem to be the desired result.
Ryan Chouinard
The question mentioned TDD, in which you write failing tests against code that doesn't exist.
Clay Hinson