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->skipUnless($this->isMySQLAvailable());
    // $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!