问题
With pytest, one can mark tests using a decorator
@pytest.mark.slow
def some_slow_test():
pass
Then, from the command line, one can tell pytest to skip the tests marked "slow"
pytest -k-slow
If I have an additional tag:
@pytest.mark.long
def some_long_test()
pass
I would like to be able to skip both long AND slow tests. I've tried this:
pytest -k-slow -k-long
and this:
pytest -k-slow,long
And neither seems to work.
At the command line, how do I tell pytest to skip both the slow AND the long tests?
回答1:
Additionally, with the recent addition of the "-m" command line option you should be able to write:
py.test -m "not (slow or long)"
IOW, the "-m" option accepts an expression which can make use of markers as boolean values (if a marker does not exist on a test function it's value is False, if it exists, it is True).
回答2:
Looking through the pytest
code (mark.py
) and further experimentation shows the following seems to work:
pytest -k "-slow -long"
(Using the --collect-only
option speeds up experimentation)
回答3:
It's also possible to stack the mark decorators.
@pytest.mark.slow
@pytest.mark.main
def test_myfunction():
pass
I then called py.test -m "slow and main"
and only the tests with both decorators were called.
py.test -m "not (slow and main)"
resulted in the other tests running
来源:https://stackoverflow.com/questions/7395444/pytest-deselecting-tests