How to skip tests in PyTest – skip, skipIf, xfail

HOME

import pytest


@pytest.mark.skip(reason="In Progress")
def test_addition():
    a = 6
    b = 5
    c = 11

    assert a + b == 11, "Sum is not 11"
    assert a + b == c, "Sum of a and b is not equal to c"


@pytest.mark.skipif(15-4>10, reason="Conditional")
def test_subtraction():
    x = 15
    y = 4
    z = 10

    assert x - y == z, "Subtract y from x is equal to z"


@pytest.mark.xfail
def test_multiplication():
    a = 6
    b = 5
    c = 30

    assert a * b == c, "Product of 5 and 6 is not 30"


def test_division():
    a = 16
    b = 2
    c = 8

    assert a / b == c, "Division of 2 by 6 is not 3"

pytest test_skip_demo.py

import pytest


@pytest.mark.skip("Development in progress")
class test_skip_class:

    def test_addition(self):
        a = 10.44
        b = 5.56
        c = 16.00

        assert a + b == 16.00, "Sum is not 16"

    def test_subtraction(self):
        x = 15
        y = 4
        z = 10

        assert x - y == z, "Subtract y from x is equal to z"

Leave a comment