This article has participated in the activity of “New person creation Ceremony”, and started the road of digging gold creation together.

2.13 Skip and Xfail process unsuccessful test cases

Flag test functions that cannot run on certain platforms or that you want to fail so that PyTest can handle them accordingly and present a summary of the test session while keeping the test suite green. Skip means that you only expect the test to pass if certain conditions are met, otherwise PyTest should skip running the test entirely.

A common example is that skipping Windows-only tests on non-Windows platforms xfail means that you expect the test to fail for some reason. Another common example is testing for features that haven’t been implemented or bugs that haven’t been fixed.

When a test passes, despite an expected failure (marked with PyTest.mark.xfail), it is an Xpass and will be reported in the test summary.

2.13.1 Skipping test Cases

1. Decorator flag skipped and pass the reason for skipping:

import pytest
@pytest.mark.skip(reason="no way of currently testing this")
def test_the_unknown() :.Copy the code

2. Pytest. skip Mandatory skip

def test_function() :
    if not valid_config():
        pytest.skip("unsupported configuration")
Copy the code

3. Skip use cases in parameterization:

import pytest
@pytest.mark.parametrize("i".range(50))
def test_num(i) :
    if i in (17.25):
        pytest.skip("Skip use cases")
Copy the code

4. Skipping platforms is all use cases for WIN

import sys
import pytest
    if not sys.platform.startswith("win"):
    pytest.skip("skipping windows-only tests", allow_module_level=True)
Copy the code

5. Skip use cases conditionally

If you want to conditionally skip something, here is an example of test functions marked to skip when running on interpreters prior to Python3.10:

import sys
@pytest.mark.skipif(sys.version_info < (3.10), reason="The requires python3.10 or who ˓ -")
def test_function() :.Copy the code

6. Skip certain test cases based on certain conditions

Skipif flags are shared between modules (py files) to skip some tests in modules based on certain conditions:

# content of test_mymodule.py
import mymodule
minversion = pytest.mark.skipif(mymodule.__versioninfo__ < (1.1), reason="At further mymodule 1.1 required" )

@minversion
def test_function() :
    pass
Copy the code
# test_myothermodule.py
from test_mymodule import minversion

@minversion
def test_anotherfunction() :
    pass
Copy the code

7. Skip a class and use the skipif tag on the class

import pytest, sys
@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"
Copy the code

8. Skip all test functions for a module, you can use global test flags

import pytest
# test_module.py
pytestmark = pytest.mark.skipif(...)
Copy the code

9. Skip files or directories: Simply pytest followed by the use case for the corresponding directory

2.13.2 Marking test cases as failures

1. Use the Xfail flag to indicate that you want the test to fail:

@pytest.mark.xfail
def test_function() :.Copy the code

2. The configuration fails during the test

No additional code is executed after the pytest.xfail() call. This is because it is implemented internally by throwing a known exception.

import pytest
@pytest.mark.parametrize("i".range(50))
def test_num(i) :
    if i in (17.25):
        pytest.xfail("Expected failure")
Copy the code

3. Failure under certain conditions or reasons

@pytest.mark.xfail(sys.platform == "win32", reason="bug in a 3rd party library")
def test_function() :.Copy the code

4. Disable running marked use cases

@pytest.mark.xfail(run=False)
def test_function() :.Copy the code

5. Set strict to make the test case of Xfail fail

import pytest
@pytest.mark.xfail(strict=True)
def test_function() :
    pass
Copy the code
[pytest]
xfail_strict=true
Copy the code

6. Related code examples

import pytest
xfail = pytest.mark.xfail
@xfail
def test_hello() :
    assert 0
@xfail(run=False)
def test_hello2() :
    assert 0
@xfail("hasattr(os, 'sep')")
def test_hello3() :
    assert 0
@xfail(reason="bug 110")
def test_hello4() :
    assert 0
@xfail('pytest.__version__[0] ! = "17" ')
def test_hello5() :
    assert 0
def test_hello6() :
    pytest.xfail("reason")
@xfail(raises=IndexError)
def test_hello7() :
    x = []
    x[1] = 1
Copy the code
C:\Users\ MC \Desktop\ Python foundation > pytest-rx test_module.py = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =testSession starts = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = platform win32 - Python 3.9.6, Pytest - 7.1.1, pluggy - 0.13.1 rootdir: C:\Users\ MC \Desktop\ Python Basic collected 7 items test_module.py XXXXXXX [100%] ===================================================================== shorttest summary info ======================================================================
XFAIL test_module.py::test_hello
XFAIL test_module.py::test_hello2
  reason: [NOTRUN]
XFAIL test_module.py::test_hello3
  condition: hasattr(os, 'sep') XFAIL test_module.py::test_hello4 bug 110 XFAIL test_module.py::test_hello5 condition: pytest.__version__[0] ! ="17"
XFAIL test_module.py::test_hello6
  reason: reason
XFAIL test_module.py::test_hello7
======================================================================== 7 xfailed in0.19 s = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =Copy the code

2.13.3 Parameterized Skip/Exit

import sys
import pytest
@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 ˓ -"))],)
def test_increment(n, expected) :
    assert n + 1 == expected
Copy the code
C:\Users\mc\Desktop\python>pytest -vs test_module.py = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =testSession starts = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = platform win32 - Python 3.9.6, Pytest-7.1.1, plugy-0.13.1 -- c:\python39\python.exe cacheDir:.pytest_Cache Rootdir: C:\Users\mc\Desktop\python collected 7 items test_module.py::test_increment[1-2] PASSED test_module.py::test_increment[1-0] XFAIL test_module.py::test_increment[1-3] XFAIL (some bug) test_module.py::test_increment[2-3] PASSED test_module.py::test_increment[3-4] PASSED To be deflected or deflected. To be deflected or deflected. Test_module. py::test_increment[4-5] PASSED test_module.py::test_increment[10-11] SKIPPED (py2k˓→). ============================================================= 4 passed, 1 skipped, 2 xfailedin0.12 s = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =Copy the code