Skip/Xfail Tests in Pytest

If we need to skip any test cases while executing the test suite then we need to use a pytest decorator “@pytest.mark.skip” for test methods.

If we mark xfail “@pytest.mark.xfail” for the test methods then pytest will execute those test cases but will not consider those cases as pass or fail.

Even though those cases will not print as failed cases in output in any of those cases are failed while executing.


test_Pytestskip.py

import pytest


def test_methodA():
    print("This is method A")


@pytest.mark.skip
def test_methodB():
    print("This is method B")


@pytest.mark.xfail
def test_methodC():
    print("This is method C")


@pytest.mark.xfail
def test_methodD():
    print("This is method D")
    assert False


def test_methodE():
    print("This is method E")





Run the pytest file test_Pytestskip.py

pytest -v test_Pytestskip.py

In this test file we have totally 5 test cases for one test case we have marked with skip decorator and for another 2 test cases we have marked with xfail. Out of these two test cases which are marked as xfail for one test case we are failing it by using assert False.

OutPut data

After executing the above file output looks like as below.

admin@admin-MacBook skiptests % pytest -v test_Pytestskip.py
============================================================================ test session starts ============================================================================
platform darwin -- Python 3.8.3, pytest-5.4.3, py-1.8.1, pluggy-0.13.1 -- /Library/Frameworks/Python.framework/Versions/3.8/bin/python3.8
cachedir: .pytest_cache
rootdir: /Users/admin/Documents/Skill2Lead/Tutorials/pytest/skiptests
plugins: rerunfailures-9.0, allure-pytest-2.8.16, ordering-0.6
collected 5 items                                                                                                                                                           

test_Pytestskip.py::test_methodA PASSED                                                                                                                               [ 20%]
test_Pytestskip.py::test_methodB SKIPPED                                                                                                                              [ 40%]
test_Pytestskip.py::test_methodC XPASS                                                                                                                                [ 60%]
test_Pytestskip.py::test_methodD XFAIL                                                                                                                                [ 80%]
test_Pytestskip.py::test_methodE PASSED                                                                                                                               [100%]

============================================================ 2 passed, 1 skipped, 1 xfailed, 1 xpassed in 0.06s =============================================================
admin@admin-MacBook skiptests %