user2249625
user2249625

Reputation: 21

pytest not skipping unmarked tests

We have a marked test that we expect to be not executed because py.test was invoked with another marker, yet the test is getting executed.

e.g.

@pytest.mark.stress
def test_one(some_fixture):
     pass

@pytest.mark.myplatform
def test_two(some_fixture):
     pass

If I run pytest with --collectonly -m "myplatform and (not stress)" as an experiment, I see that I can work around the issue. I am assuming that using the fixture is in some way changing the way the markers are evaluated, but we assumed that using fixtures wouldn't effect the way tests are collected with markers. There is code in the fixture to look at the markers , but we don't change the pytest args in any way. Chris

Upvotes: 2

Views: 777

Answers (3)

naa-rasooa
naa-rasooa

Reputation: 11

Very late contribution but I got into similar problem today.

In my case, the fixture somefixture was paramatrized with pytest.param(a_param, marks=[pytest.mark.myplatform]. So it was collected as well.

Upvotes: 0

Dmitry Tokarev
Dmitry Tokarev

Reputation: 2089

Try -k flag instead and keep same filter logic "myplatform and not stress".

https://pytest.org/en/latest/example/markers.html#using-k-expr-to-select-tests-based-on-their-name

Upvotes: 1

thinwybk
thinwybk

Reputation: 4753

The marker based test selection/unselection works to restrict the tests run to explicitly marked tests. You are just not able to recognize it if you use the --collectonly option (in examples below always collected 3 items).

Consider the test file test_markers.py:

import pytest

@pytest.mark.stress
def test_stress():
     pass

@pytest.mark.myplatform
def test_myplatform():
     pass

def test_unmarked():
     pass

If you want to execute the "stress" tests only use (-v for verbose output)

pytest test_markers.py -v -m stress

you get the following output:

collected 3 items

test_markers.py::test_stress PASSED

If you want to execute the "stress" tests and the unmarked tests use:

pytest test_markers.py -v -m "not myplatform"

which gives you that output:

collected 3 items

test_markers.py::test_stress PASSED
test_markers.py::test_unmarked PASSED

Upvotes: 0

Related Questions