I would like to exclude a list (about 5 items) of tests with py.test.
I would like to give this list to py.test via the command line.
I would like to avoid to modify the source.
How to do that?
You could use tests selecting expression, option is -k. If you have following tests:
def test_spam():
pass
def test_ham():
pass
def test_eggs():
pass
invoke pytest with:
pytest -v -k 'not spam and not ham' tests.py
you will get:
collected 3 items
pytest_skip_tests.py::test_eggs PASSED [100%]
=================== 2 tests deselected ===================
========= 1 passed, 2 deselected in 0.01 seconds =========
You could get this to work by creating a conftest.py file:
# content of conftest.py
import pytest
def pytest_addoption(parser):
parser.addoption("--skiplist", action="store_true",
default="", help="skip listed tests")
def pytest_collection_modifyitems(config, items):
tests_to_skip = config.getoption("--skiplist")
if not tests_to_skip:
# --skiplist not given in cli, therefore move on
return
skip_listed = pytest.mark.skip(reason="included in --skiplist")
for item in items:
if item.name in tests_to_skip:
item.add_marker(skip_listed)
You would use it with:
$ pytest --skiplist test1 test2
Note that if you always skip the same test the list can be defined in conftest.
See also this useful link
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With