Disabling test functions and classes

py.test provides a simple mechanism to disable some tests upon certain conditions. This is called test skipping, and the pytest package provides the @mark.skipif decorator for that purpose. If a single test function or a whole test class decorator needs to be skipped upon certain conditions, you need to define it with this decorator and some expression that verifies if the expected condition was met. Here is an example from the official documentation that skips running the whole test case class if the test suite is executed on Windows:

import pytest 
 
@pytest.mark.skipif( 
    sys.platform == 'win32', 
    reason="does not run on windows" 
) 
class TestPosixCalls: 
    def test_function(self): 
        "will not be setup or run under 'win32' platform"

You can, of course, predefine the skipping condition in order to share them across your testing modules, as follows:

import pytest 
 
skipwindows = pytest.mark.skipif( 
    sys.platform == 'win32', 
    reason="does not run on windows" 
) 
 
@skip_windows 
class TestPosixCalls: 
    def test_function(self): 
        "will not be setup or run under 'win32' platform" 

If test is marked in such a way, it will not be executed at all. However, in some cases, you want to run an execute specific test that is expected to fail under known conditions. For this purpose, a different decorator is provided. This is called @mark.xfail, and it ensures that the test is always run, but it should fail at some point if the predefined condition occurs, as follows:

import pytest 
 
@pytest.mark.xfail( 
sys.platform == 'win32', 
    reason="does not run on windows" 
) 
class TestPosixCalls: 
 
    deftest_function(self): 
        "it must fail under windows" 

Using xfail is much stricter than skipif. The test is always executed, and if it does not fail when it is expected to do so, then the whole execution of the test suite will be marked as a failure.