收集 py.test 个带有标记的测试信息
Collect py.test tests info with markers
我正在使用 py.test,我想获取包含标记信息的测试列表。
当我使用 --collect-only 标志时,我得到了测试函数。有没有办法也为每个测试获取分配的标记?
根据 Frank T 的回答,我创建了一个解决方法代码示例:
from _pytest.mark import MarkInfo, MarkDecorator
import json
def pytest_addoption(parser):
parser.addoption(
'--collect-only-with-markers',
action='store_true',
help='Collect the tests with marker information without executing them'
)
def pytest_collection_modifyitems(session, config, items):
if config.getoption('--collect-only-with-markers'):
for item in items:
data = {}
# Collect some general information
if item.cls:
data['class'] = item.cls.__name__
data['name'] = item.name
if item.originalname:
data['originalname'] = item.originalname
data['file'] = item.location[0]
# Get the marker information
for key, value in item.keywords.items():
if isinstance(value, (MarkDecorator, MarkInfo)):
if 'marks' not in data:
data['marks'] = []
data['marks'].append(key)
print(json.dumps(data))
# Remove all items (we don't want to execute the tests)
items.clear()
我认为 pytest 没有内置行为来列出测试函数以及这些测试的标记信息。 --markers
命令列出所有已注册的标记,但这不是您想要的。我简要地查看了 list of pytest plugins,但没有看到任何看起来相关的内容。
您可以编写自己的 pytest 插件来列出测试以及标记信息。 Here 是编写 pytest 插件的文档。
我会尝试使用 "pytest_collection_modifyitems" hook. It is passed a list of all tests that are collected, and it doesn't need to modify them. (Here 是所有挂钩的列表。)
如果您知道要查找的标记的名称,则传递给该挂钩的测试有一个 get_marker()
方法(请参阅 this code for example). When I was looking through that code, I could not find an official API for listing all markers. I found this to get the job done: test.keywords.__dict__['_markers']
(see here and here)。
您可以通过 request.function.pytestmark
对象中的 name
属性找到标记
@pytest.mark.scenarious1
@pytest.mark.scenarious2
@pytest.mark.scenarious3
def test_sample():
pass
@pytest.fixture(scope='function',autouse=True)
def get_markers():
print([marker.name for marker in request.function.pytestmark])
>>> ['scenarious3', 'scenarious2', 'scenarious1']
请注意,默认情况下它们以相反的顺序列出。
我正在使用 py.test,我想获取包含标记信息的测试列表。 当我使用 --collect-only 标志时,我得到了测试函数。有没有办法也为每个测试获取分配的标记?
根据 Frank T 的回答,我创建了一个解决方法代码示例:
from _pytest.mark import MarkInfo, MarkDecorator
import json
def pytest_addoption(parser):
parser.addoption(
'--collect-only-with-markers',
action='store_true',
help='Collect the tests with marker information without executing them'
)
def pytest_collection_modifyitems(session, config, items):
if config.getoption('--collect-only-with-markers'):
for item in items:
data = {}
# Collect some general information
if item.cls:
data['class'] = item.cls.__name__
data['name'] = item.name
if item.originalname:
data['originalname'] = item.originalname
data['file'] = item.location[0]
# Get the marker information
for key, value in item.keywords.items():
if isinstance(value, (MarkDecorator, MarkInfo)):
if 'marks' not in data:
data['marks'] = []
data['marks'].append(key)
print(json.dumps(data))
# Remove all items (we don't want to execute the tests)
items.clear()
我认为 pytest 没有内置行为来列出测试函数以及这些测试的标记信息。 --markers
命令列出所有已注册的标记,但这不是您想要的。我简要地查看了 list of pytest plugins,但没有看到任何看起来相关的内容。
您可以编写自己的 pytest 插件来列出测试以及标记信息。 Here 是编写 pytest 插件的文档。
我会尝试使用 "pytest_collection_modifyitems" hook. It is passed a list of all tests that are collected, and it doesn't need to modify them. (Here 是所有挂钩的列表。)
如果您知道要查找的标记的名称,则传递给该挂钩的测试有一个 get_marker()
方法(请参阅 this code for example). When I was looking through that code, I could not find an official API for listing all markers. I found this to get the job done: test.keywords.__dict__['_markers']
(see here and here)。
您可以通过 request.function.pytestmark
对象中的 name
属性找到标记
@pytest.mark.scenarious1
@pytest.mark.scenarious2
@pytest.mark.scenarious3
def test_sample():
pass
@pytest.fixture(scope='function',autouse=True)
def get_markers():
print([marker.name for marker in request.function.pytestmark])
>>> ['scenarious3', 'scenarious2', 'scenarious1']
请注意,默认情况下它们以相反的顺序列出。