I'm currently using the
@pytest.mark.skip
annotation above my function def to tell pytest to always skip this test, but I only want it to skip the test when running all tests. If I run pytest with the -k parameter that tells it to run specific tests, when it matches the annotated function, it is skipping it. But I don't want it to skip the test in that case. Only when running all test, as in, when not using the -k option. Is there a way to do that?
The documentation seems to have some ideas on that front. In particular:
Alternatively, it is also possible to skip imperatively during test execution or setup by calling the pytest.skip(reason) function:
def test_function(): if not valid_config(): pytest.skip("unsupported configuration")
The imperative method is useful when it is not possible to evaluate the skip condition during import time.
You could combine that with some hacky introspection of the request
fixture, like this:
import pytest
def test_function(request):
if 'test_function' not in request.config.known_args_namespace.keyword:
pytest.skip("This test is disabled")
That gets us the following behavior:
$ pytest -v
[...]
test_conditions.py::test_function SKIPPED (This test is disabled) [100%]
And with -k
:
$ pytest -v -k test_function
[...]
test_conditions.py::test_function PASSED [100%]
Note that this requires that the argument to -k
matches the function name (or whatever we put in our if
condition) exactly.
Instead of hanging the conditional on the keyword argument, you could instead add a custom command line option, which is probably a more robust solution since it doesn't involve poking about in pytest internals (so, you would have something like pytest --select-my-test ...
).