0

I know there is an option in PHPUnit to stop-on-failure but I don't want it to stop when any test fails, just when this specific test fails.

For example, in my setUp I connect to a DB, and in the first test I check if it connected to the correct DB. If that fails, I can't run the rest of the tests.

1 Answer 1

1

Use the @depends feature of PHP.

If a test depends on another, it will only be executed if that other test succeeded. Otherwise it will be skipped. This allows you to pinpoint problems better.

Usage: Add a PHPDOC block at the top of the test function that should only be executed when the other test is successful, and add a line @depends testConnectToDb.

See http://phpunit.de/manual/current/en/appendixes.annotations.html#appendixes.annotations.depends and http://phpunit.de/manual/current/en/writing-tests-for-phpunit.html#writing-tests-for-phpunit.test-dependencies for some more details.

2
  • That's good, but then I need to add one of those to every single method except the first? Is that the only way?
    – mpen
    Commented Oct 31, 2013 at 1:21
  • You can also use the assertPreCondition (and assertPostCondition) functions in PHPUnit to run assertions before (or after) the test function. You can also mark tests as skipped in setUp if the connection fails, so you need not check it with a test (I recommend adding an explanation test to every case of skipping to make it clear WHY the test is skipped).
    – Sven
    Commented Oct 31, 2013 at 8:32

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