How to use skip and xfail to deal with tests that cannot succeed¶
You can mark test functions that cannot be run on certain platformsor that you expect to fail so pytest can deal with them accordingly andpresent a summary of the test session, while keeping the test suitegreen.
Askip means that you expect your test to pass only if some conditions are met,otherwise pytest should skip running the test altogether. Common examples are skippingwindows-only tests on non-windows platforms, or skipping tests that depend on an externalresource which is not available at the moment (for example a database).
Anxfail means that you expect a test to fail for some reason.A common example is a test for a feature not yet implemented, or a bug not yet fixed.When a test passes despite being expected to fail (marked withpytest.mark.xfail
),it’s anxpass and will be reported in the test summary.
pytest
counts and listsskip andxfail tests separately. Detailedinformation about skipped/xfailed tests is not shown by default to avoidcluttering the output. You can use the-r
option to see detailscorresponding to the “short” letters shown in the test progress:
pytest -rxXs# show extra info on xfailed, xpassed, and skipped tests
More details on the-r
option can be found by runningpytest-h
.
(SeeBuiltin configuration file options)
Skipping test functions¶
The simplest way to skip a test function is to mark it with theskip
decoratorwhich may be passed an optionalreason
:
@pytest.mark.skip(reason="no way of currently testing this")deftest_the_unknown():...
Alternatively, it is also possible to skip imperatively during test execution or setupby calling thepytest.skip(reason)
function:
deftest_function():ifnotvalid_config():pytest.skip("unsupported configuration")
The imperative method is useful when it is not possible to evaluate the skip conditionduring import time.
It is also possible to skip the whole module usingpytest.skip(reason,allow_module_level=True)
at the module level:
importsysimportpytestifnotsys.platform.startswith("win"):pytest.skip("skipping windows-only tests",allow_module_level=True)
Reference:pytest.mark.skip
skipif
¶
If you wish to skip something conditionally then you can useskipif
instead.Here is an example of marking a test function to be skippedwhen run on an interpreter earlier than Python3.10:
importsys@pytest.mark.skipif(sys.version_info<(3,10),reason="requires python3.10 or higher")deftest_function():...
If the condition evaluates toTrue
during collection, the test function will be skipped,with the specified reason appearing in the summary when using-rs
.
You can shareskipif
markers between modules. Consider this test module:
# content of test_mymodule.pyimportmymoduleminversion=pytest.mark.skipif(mymodule.__versioninfo__<(1,1),reason="at least mymodule-1.1 required")@minversiondeftest_function():...
You can import the marker and reuse it in another test module:
# test_myothermodule.pyfromtest_mymoduleimportminversion@minversiondeftest_anotherfunction():...
For larger test suites it’s usually a good idea to have one filewhere you define the markers which you then consistently applythroughout your test suite.
Alternatively, you can usecondition strings instead of booleans, but they can’t be shared between modules easilyso they are supported mainly for backward compatibility reasons.
Reference:pytest.mark.skipif
Skip all test functions of a class or module¶
You can use theskipif
marker (as any other marker) on classes:
@pytest.mark.skipif(sys.platform=="win32",reason="does not run on windows")classTestPosixCalls:deftest_function(self):"will not be setup or run under 'win32' platform"
If the condition isTrue
, this marker will produce a skip result foreach of the test methods of that class.
If you want to skip all test functions of a module, you may use thepytestmark
global:
# test_module.pypytestmark=pytest.mark.skipif(...)
If multipleskipif
decorators are applied to a test function, itwill be skipped if any of the skip conditions is true.
Skipping files or directories¶
Sometimes you may need to skip an entire file or directory, for example if thetests rely on Python version-specific features or contain code that you do notwish pytest to run. In this case, you must exclude the files and directoriesfrom collection. Refer toCustomizing test collection for moreinformation.
Skipping on a missing import dependency¶
You can skip tests on a missing import by usingpytest.importorskipat module level, within a test, or test setup function.
docutils=pytest.importorskip("docutils")
Ifdocutils
cannot be imported here, this will lead to a skip outcome ofthe test. You can also skip based on the version number of a library:
docutils=pytest.importorskip("docutils",minversion="0.3")
The version will be read from the specifiedmodule’s__version__
attribute.
Summary¶
Here’s a quick guide on how to skip tests in a module in different situations:
Skip all tests in a module unconditionally:
pytestmark=pytest.mark.skip("all tests still WIP")
Skip all tests in a module based on some condition:
pytestmark=pytest.mark.skipif(sys.platform=="win32",reason="tests for linux only")
Skip all tests in a module if some import is missing:
pexpect=pytest.importorskip("pexpect")
XFail: mark test functions as expected to fail¶
You can use thexfail
marker to indicate that youexpect a test to fail:
@pytest.mark.xfaildeftest_function():...
This test will run but no traceback will be reported when it fails. Instead, terminalreporting will list it in the “expected to fail” (XFAIL
) or “unexpectedlypassing” (XPASS
) sections.
Alternatively, you can also mark a test asXFAIL
from within the test or its setup functionimperatively:
deftest_function():ifnotvalid_config():pytest.xfail("failing configuration (but should work)")
deftest_function2():importslow_moduleifslow_module.slow_function():pytest.xfail("slow_module taking too long")
These two examples illustrate situations where you don’t want to check for a conditionat the module level, which is when a condition would otherwise be evaluated for marks.
This will maketest_function
XFAIL
. Note that no other code is executed afterthepytest.xfail()
call, differently from the marker. That’s because it is implementedinternally by raising a known exception.
Reference:pytest.mark.xfail
condition
parameter¶
If a test is only expected to fail under a certain condition, you can passthat condition as the first parameter:
@pytest.mark.xfail(sys.platform=="win32",reason="bug in a 3rd party library")deftest_function():...
Note that you have to pass a reason as well (see the parameter description atpytest.mark.xfail).
reason
parameter¶
You can specify the motive of an expected failure with thereason
parameter:
@pytest.mark.xfail(reason="known parser issue")deftest_function():...
raises
parameter¶
If you want to be more specific as to why the test is failing, you can specifya single exception, or a tuple of exceptions, in theraises
argument.
@pytest.mark.xfail(raises=RuntimeError)deftest_function():...
Then the test will be reported as a regular failure if it fails with anexception not mentioned inraises
.
run
parameter¶
If a test should be marked as xfail and reported as such but should not beeven executed, use therun
parameter asFalse
:
@pytest.mark.xfail(run=False)deftest_function():...
This is specially useful for xfailing tests that are crashing the interpreter and should beinvestigated later.
strict
parameter¶
BothXFAIL
andXPASS
don’t fail the test suite by default.You can change this by setting thestrict
keyword-only parameter toTrue
:
@pytest.mark.xfail(strict=True)deftest_function():...
This will makeXPASS
(“unexpectedly passing”) results from this test to fail the test suite.
You can change the default value of thestrict
parameter using thexfail_strict
ini option:
[pytest]xfail_strict=true
Ignoring xfail¶
By specifying on the commandline:
pytest --runxfail
you can force the running and reporting of anxfail
marked testas if it weren’t marked at all. This also causespytest.xfail()
to produce no effect.
Examples¶
Here is a simple test file with the several usages:
importpytestxfail=pytest.mark.xfail@xfaildeftest_hello():assert0@xfail(run=False)deftest_hello2():assert0@xfail("hasattr(os, 'sep')")deftest_hello3():assert0@xfail(reason="bug 110")deftest_hello4():assert0@xfail('pytest.__version__[0] != "17"')deftest_hello5():assert0deftest_hello6():pytest.xfail("reason")@xfail(raises=IndexError)deftest_hello7():x=[]x[1]=1
Running it with the report-on-xfail option gives this output:
! pytest -rx xfail_demo.py=========================== test session starts ============================platform linux -- Python 3.x.y, pytest-6.x.y, py-1.x.y, pluggy-1.x.ycachedir: $PYTHON_PREFIX/.pytest_cacherootdir: $REGENDOC_TMPDIR/examplecollected 7 itemsxfail_demo.pyxxxxxxx[100%]========================= short test summary info ==========================XFAIL xfail_demo.py::test_helloXFAIL xfail_demo.py::test_hello2 reason: [NOTRUN]XFAIL xfail_demo.py::test_hello3 condition: hasattr(os, 'sep')XFAIL xfail_demo.py::test_hello4 bug 110XFAIL xfail_demo.py::test_hello5 condition: pytest.__version__[0] != "17"XFAIL xfail_demo.py::test_hello6 reason: reasonXFAIL xfail_demo.py::test_hello7============================7 xfailed in 0.12s ============================
Skip/xfail with parametrize¶
It is possible to apply markers like skip and xfail to individualtest instances when using parametrize:
importsysimportpytest@pytest.mark.parametrize(("n","expected"),[(1,2),pytest.param(1,0,marks=pytest.mark.xfail),pytest.param(1,3,marks=pytest.mark.xfail(reason="some bug")),(2,3),(3,4),(4,5),pytest.param(10,11,marks=pytest.mark.skipif(sys.version_info>=(3,0),reason="py2k")),],)deftest_increment(n,expected):assertn+1==expected