How to skip a particular test in parametrized test functions in pytest

53 Views Asked by At

I have a pytest function that has markers @pytest.mark.win_11 and @pytest.mark.parametrize('os,result',[('win_11','result1'),('win_8','result2')])

When I run test as 'pytest -m win_11', it is running the tests for both set of parameters. I want to run the test only when the os version is win_11.

So, can we combine both marker and parametrize option for single test? Is there a way to skip test with os = win_8 when test is run as 'pytest -m win_11' ?

The code looks as follows:

import pytest

@pytest.mark.win_11
@pytest.mark.parametrize('os_version,result',[('win_11','result1'),('win_8','result2')])
def test_set1_1(os_version, expected_result):
    # some code
1

There are 1 best solutions below

0
On

One option is to assign the marker dynamically through @pytest.mark.parametrize

def parametrize():
    for version, result in [('win_11', 'result1'), ('win_8', 'result2')]:
        marks = []
        if version == 'win_11':
            marks.append(pytest.mark.win_11)
        else:
            marks.append(pytest.mark.win_8)
        yield pytest.param(version, result, marks=marks)


@pytest.mark.parametrize('os_version, result', parametrize())
def test_set1_1(os_version, result):
    print(os_version, result)

Without marker pytest

============================= test session starts =============================
collecting ... collected 2 items

example.py::test_set1_1[win_11-result1] PASSED                           [ 50%]
win_11 result1

example.py::test_set1_1[win_8-result2] PASSED                            [100%]
win_8 result2

With pytest -m win_11

============================= test session starts =============================
collecting ... collected 2 items / 1 deselected / 1 selected

example.py::test_set1_1[win_11-result1] PASSED                           [100%]
win_11 result1