Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify several marks for the pytest command

Tags:

python

pytest

Reading http://doc.pytest.org/en/latest/example/markers.html I see the example of including or excluding certain python tests based on a mark.

Including:

pytest -v -m webtest

Excluding:

pytest -v -m "not webtest"

What if I would like to specify several marks for both include and exclude?

like image 285
Alex Avatar asked Mar 19 '20 16:03

Alex


People also ask

How do you mark a test in pytest?

To use markers, we have to import pytest module in the test file. We can define our own marker names to the tests and run the tests having those marker names. -m <markername> represents the marker name of the tests to be executed.

How do you pytest a specific function?

Running pytest We can run a specific test file by giving its name as an argument. A specific function can be run by providing its name after the :: characters. Markers can be used to group tests. A marked grouped of tests is then run with pytest -m .


1 Answers

Use and/or to combine multiple markers, same as for -k selector. Example test suite:

import pytest


@pytest.mark.foo
def test_spam():
    assert True


@pytest.mark.foo
def test_spam2():
    assert True


@pytest.mark.bar
def test_eggs():
    assert True


@pytest.mark.foo
@pytest.mark.bar
def test_eggs2():
    assert True


def test_bacon():
    assert True

Selecting all tests marked with foo and not marked with bar

$ pytest -q --collect-only -m "foo and not bar"
test_mod.py::test_spam
test_mod.py::test_spam2

Selecting all tests marked neither with foo nor with bar

$ pytest -q --collect-only -m "not foo and not bar"
test_mod.py::test_bacon

Selecting tests that are marked with any of foo, bar

$ pytest -q --collect-only -m "foo or bar"
test_mod.py::test_spam
test_mod.py::test_spam2
test_mod.py::test_eggs
test_mod.py::test_eggs2
like image 73
hoefling Avatar answered Sep 30 '22 13:09

hoefling