Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exclude a list of tests with py.test but on the command line?

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?

like image 477
guettli Avatar asked Dec 07 '17 09:12

guettli


2 Answers

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 =========
like image 102
georgexsh Avatar answered Oct 21 '22 19:10

georgexsh


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

like image 28
Jacques Gaudin Avatar answered Oct 21 '22 20:10

Jacques Gaudin