Pytest - Xfail/Skip Tests



In this chapter, we will learn about the Skip and Xfail tests in Pytest.

Now, consider the below situations −

  • A test is not relevant for some time due to some reasons.
  • A new feature is being implemented and we already added a test for that feature.

In these situations, we have the option to xfail the test or skip the tests.

Pytest will execute the xfailed test, but it will not be considered as part failed or passed tests. Details of these tests will not be printed even if the test fails (remember pytest usually prints the failed test details). We can xfail tests using the following marker −

@pytest.mark.xfail

Skipping a test means that the test will not be executed. We can skip tests using the following marker −

@pytest.mark.skip

Later, when the test becomes relevant we can remove the markers.

Example - Skipping Tests

Edit the test_compare.py we already have to include the xfail and skip markers −

test_compare.py

import pytest
@pytest.mark.xfail
@pytest.mark.great
def test_greater():
   num = 100
   assert num > 100

@pytest.mark.xfail
@pytest.mark.great
def test_greater_equal():
   num = 100
   assert num >= 100

@pytest.mark.skip
@pytest.mark.others
def test_less():
   num = 100
   assert num < 200

Output

Execute the test using the following command −

pytest test_compare.py -v

Upon execution, the above command will generate the following result −

============================ test session starts ================================
platform win32 -- Python 3.14.2, pytest-9.0.2, pluggy-1.6.0 
-- C:\Users\mahes\AppData\Local\Programs\Python\Python314\python.exe
cachedir: .pytest_cache
rootdir: D:\Projects\python\myenv\automation
collected 3 items

test_compare.py::test_greater XFAIL                                        [ 33%]
test_compare.py::test_greater_equal XPASS                                  [ 66%]
test_compare.py::test_less SKIPPED (unconditional skip)                    [100%]
============================ 1 skipped, 1 xfailed, 1 xpassed, 3 warnings in 0.05s
Advertisements