Search code examples
pythonpytestpython-unittest

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?


Solution

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