Skipping test cases

Published on and tagged with simpletest  testing

Some days ago Tim Kosch├╝tzki showed in an article how you can execute only some specific test methods in (SimpleTest-based) unit tests by overriding the getTests() method.

Sometimes you not only want to skip some test methods but an entire test case. For example, the tests of the MySQL datasource test case can only be executed successfully if there is a MySQL database (or else you will get many errors). What do you do in such a case?

You could override the getTests() method and simply return an empty array if there is no MySQL database available:

public function getTests() {
    if (!$this->isMySQLAvailable()) {
        return array();

    return parent::getTests();

This works, but it is rather a theoretical approach. Usually you override the method which was designed for this purpose: skip. In this method you use then either the skipIf() or skipUnless() method to determine whether the test case should be skipped. So we can rewrite the example from above in the following way:

public function skip() {
    // $this->skipIf(!$this->isMySQLAvailable()); has the same effect

If you now run the test case, and you don’t have a MySQL database, you will get informed that the respective test case has been skipped.

Happy testing!

2 comments baked

  • nate

    These methods (skipIf and skipUnless) have also been extended in the CakeTestCase class, such that you can use them at the top of specific test methods.

    The code in the tests still gets executed, but all passes, fails and warnings are suppressed, and not taken into account in the test results summary.

  • cakebaker

    @nate: Thanks for your additional explanations!

© daniel hofstetter. Licensed under a Creative Commons License