Sometimes a test should be skipped for a good reason (some resource is not available for a while and mocking is not an option, or tests are assumed to be impacting each other, etc.).

Most test runners support a way to mark a test to be skipped in a proper way, so that later it won't be forgotten, being skipped unnoticed.

To demonstrate a concrete example, as part of our tests we'd like to call an API, that creates a state that we'd like to assert, and for some reasons we need to skip the test. The ApiClient.lights_on() turns on the lights in the room. But currently our test setup can assert if the lights are on/off only on Linux platforms. The ApiClient.rm_files() removes some files from a network mounted drive. But there is a bug in the API that is causing our lunches to magically disappear. Also we should not call any of the methods from this API, when the load on the server is high.

Python unittest

Python's standard library unittest module provides the skip() function decorator to decorate the test methods, or the whole test class to be skipped.

Conditional skips are also supported by skipIf() and skipUnless() decorators.

The TestCase class provides the skipTest() method as well to skip tests. It can be called in the test method itself or during the setUp.

from unittest import TestCase, skip, skipUnless
from platform import system
from app.module.apiclient import ApiClient


class ApiClientTest(TestCase):

    def setUp(self):
        if self.load_is_high():
            self.skipTest("can't call api because load is high")
        self.create_network_files()
        self.lights_off()

    def tearDown(self):
        self.remove_network_files_if_exist()
        self.lights_off() # save the planet

    @skip("can't call api because of a bug in there causing our lunch to disappear")
    def test_removes_network_files(self):
        ApiClient().rm_files()
        self.assert_network_files_removed()

    @skipUnless(system() is "Linux", "we can't assert lights are on yet")
    def test_turns_on_lights(self):
        ApiClient().lights_on()
        self.assert_lights_are_on()

PHPUnit

For PHP/HackLang projects a common test framework and test runner is PHPUnit.

The method marktTestSkipped of the PHPUnit_Framework_TestCase class can be used to skip the test.

PHPUnit provides the @requires annotation to skip a test if it requires a specific platform (OS, PHP version, extension) which is not available.

<?php
namespace Tests\App\Module;

use App\Module\ApiClient;

class ApiClientTest extends \PHPUnit_Framework_TestCase
{
    public function setUp()
    {
        if ($this->loadIsHigh()) {
            $this->markTestSkipped(
                "can't call api because load is high"
            );
        }
        $this->createNetworkFiles();
        $this->lightsOff();
    }

    public function tearDown()
    {
        $this->removeNetworkFilesIfExist();
        $this->lightsOff(); // save the planet
    }

    public function testRemovesNetworkFiles()
    {
        $this->markTestSkipped(
            "can't call api because of a bug in there causing our lunch to disappear"
        );
        (new ApiClient())->rmFiles();
        $this->assertNetworkFilesRemoved();
    }

    /**
     * @requires OS Linux
     */
    public function testTurnsOnLights()
    {
        (new ApiClient())->lightsOn();
        $this->assertLightsAreOn();
    }
}

Another way to skip tests using PHPUnit annotations, is to define a group to be excluded from tests to run, then annotate the tests to skip with that group using @group annotation.

This example adds a group to be excluded from tests in PHPUnit configuration file (phpunit.xml).

<?xml version="1.0" encoding="UTF-8" ?>
<phpunit color="true">
    <groups>
        <exclude>
            <group>skip</group>
        </exclude>
    </groups>
</phpunit>

Now tests annoated by @group skip are skipped.

<?php
namespace Tests\App\Module;

class ApiClientTest extends \PHPUnit_Framework_TestCase
{
    /**
     * @group skip
     */
    public function testRemovesNetworkFiles()
    {
        // ...
    }
}

The whole test case can be skipped in this way.

<?php
namespace Tests\App\Module;

/**
 * @group skip
 */
class ApiClientTest extends \PHPUnit_Framework_TestCase
{
    public function testRemovesNetworkFiles()
    {
        // ...
    }
}