使用 py.test 但在命令行上排除测试列表?
Exclude a list of tests with py.test but on the command line?
我想排除 py.test 的测试列表(大约 5 项)。
我想通过命令行将此列表提供给 py.test。
我想避免修改源码。
怎么做?
您可以通过创建 conftest.py
文件来实现它:
# content of conftest.py
import pytest
def pytest_addoption(parser):
parser.addoption("--skiplist", action="store_true",
default="", help="skip listed tests")
def pytest_collection_modifyitems(config, items):
tests_to_skip = config.getoption("--skiplist")
if not tests_to_skip:
# --skiplist not given in cli, therefore move on
return
skip_listed = pytest.mark.skip(reason="included in --skiplist")
for item in items:
if item.name in tests_to_skip:
item.add_marker(skip_listed)
您可以将其用于:
$ pytest --skiplist test1 test2
请注意,如果您总是跳过相同的测试,则可以在 conftest
中定义列表。
您可以使用 tests selecting expression,选项是 -k
。如果您有以下测试:
def test_spam():
pass
def test_ham():
pass
def test_eggs():
pass
调用 pytest:
pytest -v -k 'not spam and not ham' tests.py
您将获得:
collected 3 items
pytest_skip_tests.py::test_eggs PASSED [100%]
=================== 2 tests deselected ===================
========= 1 passed, 2 deselected in 0.01 seconds =========
我想排除 py.test 的测试列表(大约 5 项)。
我想通过命令行将此列表提供给 py.test。
我想避免修改源码。
怎么做?
您可以通过创建 conftest.py
文件来实现它:
# content of conftest.py
import pytest
def pytest_addoption(parser):
parser.addoption("--skiplist", action="store_true",
default="", help="skip listed tests")
def pytest_collection_modifyitems(config, items):
tests_to_skip = config.getoption("--skiplist")
if not tests_to_skip:
# --skiplist not given in cli, therefore move on
return
skip_listed = pytest.mark.skip(reason="included in --skiplist")
for item in items:
if item.name in tests_to_skip:
item.add_marker(skip_listed)
您可以将其用于:
$ pytest --skiplist test1 test2
请注意,如果您总是跳过相同的测试,则可以在 conftest
中定义列表。
您可以使用 tests selecting expression,选项是 -k
。如果您有以下测试:
def test_spam():
pass
def test_ham():
pass
def test_eggs():
pass
调用 pytest:
pytest -v -k 'not spam and not ham' tests.py
您将获得:
collected 3 items
pytest_skip_tests.py::test_eggs PASSED [100%]
=================== 2 tests deselected ===================
========= 1 passed, 2 deselected in 0.01 seconds =========