Skipping Tests With pytest

Learn how to skip Python tests using pytest module.

We'll cover the following


It is sometimes necessary to skip tests in pytest, for a similar variety of reasons: the code being tested hasn’t been written yet, the test only runs on certain interpreters or operating systems, or the test is time-consuming and should only be run under certain circumstances.

The skip() function

One way to skip tests is by using the pytest.skip() function. It accepts a single argument: a string describing why it has been skipped. This function can be called anywhere. If we call it inside a test function, the test will be skipped. If we call it at the module level, all the tests in that module will be skipped. If we call it inside a fixture, all tests that reference the fixture will be skipped.

Of course, in all these locations, it is often only desirable to skip tests if certain conditions have or have not been met. Since we can execute the skip() function at any place in Python code, we can execute it inside an if statement. We may write a test that looks as follows:

Get hands-on with 1200+ tech skills courses.