guettli
guettli

Reputation: 27806

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?

Upvotes: 24

Views: 18132

Answers (3)

Yash Dawani
Yash Dawani

Reputation: 1

The rest of the answers are great, but just an additional way would be to use -k-test_name

So, e.g:

def test_blue():
    pass

def test_black():
    pass

You can pass pytest -k-test_blue to ignore the test case test_blue.

Something like the following should be observed:

===== 1 passed, 1 deselected in 0.31 seconds =====  

Upvotes: 0

Jacques Gaudin
Jacques Gaudin

Reputation: 16958

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

Upvotes: 2

georgexsh
georgexsh

Reputation: 16624

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 =========

Upvotes: 41

Related Questions