I'm trying to upgrade from pytest 4.x to something newer. However, I'm running into issues where pytest.config
was removed and I'm not sure how to accommodate the new syntax. Specifically, I'm trying to specify a command line flag that can run or skip specific decorated tests depending on whether or not the flag is present.
This is how it works in pytest 4.x.
# common.py
integration = pytest.mark.skipif(
not pytest.config.getoption('--integration', False),
)
Then, elsewhere in the tests..
# test_something.py
from .common import integration
@integration
def test_mytest():
1 == 1
@integration
def test_other_mytest():
2 == 2
In newer versions of pytest, the above fails with:
AttributeError: module 'pytest' has no attribute 'config'
In pytest 5.x+, how can I achieve the same result as above - how can I pass in flag to pytest and run/skip certain tests based on that flag? Preferably, it would be nice to decorate them as in the example, as I have a lot of tests using the decorator syntax and it could be difficult to update them all. Any advice is much appreciated.
One option is to use a custom marker and the existing
-m
command line option. If you redefine yourintegration
decorator like this:And define the
integration
marker inpytest.ini
:Then you can run all tests:
Or only integration tests:
Or only non-integration tests:
This meets your goal of not having to update all your tests.