I have some unit tests, but I'm looking for a way to tag some specific unit tests to have them skipped unless you declare an option when you call the tests.
Example:
If I call pytest test_reports.py
, I'd want a couple specific unit tests to not be run.
But if I call pytest -<something> test_reports
, then I want all my tests to be run.
I looked into the @pytest.mark.skipif(condition)
tag but couldn't quite figure it out, so not sure if I'm on the right track or not. Any guidance here would be great!
The simplest way to skip a test is to mark it with the skip decorator which may be passed an optional reason . It is also possible to skip imperatively during test execution or setup by calling the pytest. skip(reason) function. This is useful when it is not possible to evaluate the skip condition during import time.
Skipping test functions. The simplest way to skip a test function is to mark it with the skip decorator which may be passed an optional reason : @pytest. mark.
You can disable XCTests run by Xcode by right clicking on the test symbol in the editor tray on the left. You'll get this menu, and you can select the "Disable " option. Right clicking again will allow you to re-enable. Also, as stated in user @sethf's answer, you'll see entries for currently disabled tests in your .
We are using markers with addoption in conftest.py
testcase:
@pytest.mark.no_cmd
def test_skip_if_no_command_line():
assert True
conftest.py: in function
def pytest_addoption(parser):
parser.addoption("--no_cmd", action="store_true",
help="run the tests only in case of that command line (marked with marker @no_cmd)")
in function
def pytest_runtest_setup(item):
if 'no_cmd' in item.keywords and not item.config.getoption("--no_cmd"):
pytest.skip("need --no_cmd option to run this test")
pytest call:
py.test test_the_marker
-> test will be skipped
py.test test_the_marker --no_cmd
-> test will run
The pytest documentation offers a nice example on how to skip tests marked "slow" by default and only run them with a --runslow
option:
# conftest.py
import pytest
def pytest_addoption(parser):
parser.addoption(
"--runslow", action="store_true", default=False, help="run slow tests"
)
def pytest_configure(config):
config.addinivalue_line("markers", "slow: mark test as slow to run")
def pytest_collection_modifyitems(config, items):
if config.getoption("--runslow"):
# --runslow given in cli: do not skip slow tests
return
skip_slow = pytest.mark.skip(reason="need --runslow option to run")
for item in items:
if "slow" in item.keywords:
item.add_marker(skip_slow)
We can now mark our tests in the following way:
# test_module.py
from time import sleep
import pytest
def test_func_fast():
sleep(0.1)
@pytest.mark.slow
def test_func_slow():
sleep(10)
The test test_func_fast
is always executed (calling e.g. pytest
). The "slow" function test_func_slow
, however, will only be executed when calling pytest --runslow
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With