Open In App

An Ultimate Guide To Using Pytest Skip Test And XFail

Last Updated : 08 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The Python framework used for writing and executing test cases is known as Pytest. There are some circumstances when you want to skip tests or run the tests, but not count them during PyTest run, then you can use skip and Xfail tests respectively. In this article, we will discuss how we can use Xfail and skip tests in Pytest.

XFail/ skip tests in Pytest

The syntax for Xfail Tests in Pytest

@pytest.mark.xfail

Syntax for Skip Tests in Pytest

@pytest.mark.skip

Example 1: In this example, we have created a program that has four test cases, checking floor, equality, subtraction and square root. Here, we have used the skip test on the first test case, and xfail test on the third test case.

Python
import math
import pytest

# first test case
@pytest.mark.skip
def test_floor():
   num = 7
   assert num==math.floor(6.34532)

# second test case
def test_equal():
   assert 50 == 49

# third test case
@pytest.mark.xfail
def test_difference():
   assert 99-43==57

# fourth test case
def test_square_root():
   val=8
   assert val==math.sqrt(81)

Run the Program

Now, we will run the following command in terminal.

pytest main.py

Output

pytest
pytest

Example 2: In this another example, we have defined a string, then we have created 3 unit test cases on it, for removing substrings G, e and o. Here, we have used xfail test on first test case, and skip test on third test case.

Python
import pytest
s = "Geeks For Geeks"

# first test case
@pytest.mark.xfail
def test_remove_G():
    assert s.replace('G', '') == "eeks For eeks"

# second test case
def test_remove_e():
    assert s.replace('e', '') == "Gks For Gks"

# third test case
@pytest.mark.skip
def test_remove_o():
    assert s.replace('o', '') == "Geeks Fr Geeks"

Run the Program

Now, we will run the following command in terminal.

pytest main.py

Output

Output
pytest

Next Article

Similar Reads