问题
I have several markers in my python test code:
@pytest.mark.slowtest
@pytest.mark.webtest
@pytest.mark.stagingtest
I am able to selectively run tests with a marker using for example pytest -m slowtest
How can I run unmarked tests without resorting to pytest -m "not (slowtest or webtest or stagingtest)"
?
As you can imagine, we might use other markers in the future...
回答1:
You can use following code snippet in top level conftest.py.
def pytest_runtest_setup(item):
envmarker = item.get_marker()
if envmarker:
pytest.skip("Skipping as the test has a marker")
Remember that parametrize is also a marker. If you are using parametrization, you can check for that in envmarker and skip conditionally.
回答2:
The pytest-unmarked plugin is intended to accomplish this.
PyPi's link to the project's home is broken. This is the proper link https://github.com/alyssabarela/pytest-unmarked
More info on using plugins at https://docs.pytest.org/en/latest/plugins.html
回答3:
Adding the following to your conftest will solve this for you by adding a specific mark to all unmarked test so you can then run something like pytest -m unmarked
def pytest_collection_modifyitems(items, config):
for item in items:
if not any(item.iter_markers()):
item.add_marker("unmarked")
This has the advantage over the how pytest-unmarked does it in that you can still specify other markers e.g. pytest -m 'webtest or unmarked'
. Also pytest-unmarked doesn't look maintained and emits warnings.
来源:https://stackoverflow.com/questions/39846230/how-to-run-only-unmarked-tests-in-pytest