Skip Or Disable A Test In Pytest

This post presents an interface for skipping or disabling a test in Pytest. You may want to skip or disable a test in Pytest if:

  1. the test is not yet implemented and should be marked todo
  2. the test cannot succeed
  3. the test should not be run

Disable A Test

Pytest implemented various marks that can be applied to a test case using a decorator. One mark is skip, allowing a test case to be skipped. A skipped test case will not cause the test run to fail.

When using mark.skip, include a descriptive reason indicating why the test case is to be skipped.

import pytest

@pytest.mark.skip(reason="Not implemented.")
def test_addition():
    raise NotImplemented

Skip A Test Based On Conditional Logic

Certain tests may only apply under certain conditions, for example, a specific operating system. Use skipif to include a conditional that determines if the test should be skipped.

For example, to create a test case that is run only on Windows:

@pytest.mark.skipif(sys.platform != "win32", reason="Windows only.")
def test_path():
    // asserts

Ignoring and Skipping Individual Tests

That's it! With the skip and skipif in @pytest.mark you can disable single test cases or skip test cases based on custom logic.