Pytest 运行 仅测试具有特定标记属性
Pytest run only tests with specific marker attribute
我使用 @pytest.mark
来唯一标识特定测试,因此我创建了自定义标记。
@pytest.mark.key
我是这样使用它的:
@pytest.mark.key("test-001")
def test_simple(self):
self.passing_step()
self.passing_step()
self.passing_step()
self.passing_step()
assert True
现在我想从控制台 运行 所有带有标记键“test-001”的测试。我怎样才能做到这一点?
我要找的是这样的:
pypi.org/project/pytest-jira/0.3.6
其中可以将测试映射到 Jira 密钥。我查看了 link 的源代码,但我不确定如何实现它以便我进行 运行 特定测试。假设我只想 运行 键为“test-001”的测试。
您可以 运行 pytest
使用 -m
选项 c
检查以下命令:
pytest -m 'test-001' <your test file>
Pytest 不提供开箱即用的功能。您可以使用 -m
选项按 标记名称 进行过滤,但不能按 标记属性 .
但是,您可以添加自己的选项来按键过滤。这是一个例子:
conftest.py
def pytest_configure(config):
# register your new marker to avoid warnings
config.addinivalue_line(
"markers",
"key: specify a test key"
)
def pytest_addoption(parser):
# add your new filter option (you can name it whatever you want)
parser.addoption('--key', action='store')
def pytest_collection_modifyitems(config, items):
# check if you got an option like --key=test-001
filter = config.getoption("--key")
if filter:
new_items = []
for item in items:
mark = item.get_closest_marker("key")
if mark and mark.args and mark.args[0] == filter:
# collect all items that have a key marker with that value
new_items.append(item)
items[:] = new_items
现在你运行像
pytest --key=test-001
仅 运行 具有该标记属性的测试。
请注意,这仍将显示收集的测试总数,但 运行 仅显示过滤后的测试。这是一个例子:
test_key.py
import pytest
@pytest.mark.key("test-001")
def test_simple1():
pass
@pytest.mark.key("test-002")
def test_simple2():
pass
@pytest.mark.key("test-001")
def test_simple3():
pass
def test_simple4():
pass
$ python -m pytest -v --key=test-001 test_key.py
...
collected 4 items
test_key.py::test_simple1 PASSED
test_key.py::test_simple3 PASSED
================================================== 2 passed in 0.26s ==================================================
我使用 @pytest.mark
来唯一标识特定测试,因此我创建了自定义标记。
@pytest.mark.key
我是这样使用它的:
@pytest.mark.key("test-001")
def test_simple(self):
self.passing_step()
self.passing_step()
self.passing_step()
self.passing_step()
assert True
现在我想从控制台 运行 所有带有标记键“test-001”的测试。我怎样才能做到这一点?
我要找的是这样的:
pypi.org/project/pytest-jira/0.3.6
其中可以将测试映射到 Jira 密钥。我查看了 link 的源代码,但我不确定如何实现它以便我进行 运行 特定测试。假设我只想 运行 键为“test-001”的测试。
您可以 运行 pytest
使用 -m
选项 c
检查以下命令:
pytest -m 'test-001' <your test file>
Pytest 不提供开箱即用的功能。您可以使用 -m
选项按 标记名称 进行过滤,但不能按 标记属性 .
但是,您可以添加自己的选项来按键过滤。这是一个例子:
conftest.py
def pytest_configure(config):
# register your new marker to avoid warnings
config.addinivalue_line(
"markers",
"key: specify a test key"
)
def pytest_addoption(parser):
# add your new filter option (you can name it whatever you want)
parser.addoption('--key', action='store')
def pytest_collection_modifyitems(config, items):
# check if you got an option like --key=test-001
filter = config.getoption("--key")
if filter:
new_items = []
for item in items:
mark = item.get_closest_marker("key")
if mark and mark.args and mark.args[0] == filter:
# collect all items that have a key marker with that value
new_items.append(item)
items[:] = new_items
现在你运行像
pytest --key=test-001
仅 运行 具有该标记属性的测试。
请注意,这仍将显示收集的测试总数,但 运行 仅显示过滤后的测试。这是一个例子:
test_key.py
import pytest
@pytest.mark.key("test-001")
def test_simple1():
pass
@pytest.mark.key("test-002")
def test_simple2():
pass
@pytest.mark.key("test-001")
def test_simple3():
pass
def test_simple4():
pass
$ python -m pytest -v --key=test-001 test_key.py
...
collected 4 items
test_key.py::test_simple1 PASSED
test_key.py::test_simple3 PASSED
================================================== 2 passed in 0.26s ==================================================