python-3.x - 将属性添加到junit xml pytest
问题描述
我正在尝试使用自定义属性在 pytest 中创建一个 junit xml 输出文件。我搜索了答案并找到了有关 xml_record_attribute 和 record_attribute 的信息。
def test(record_attribute):
record_attribute('index', '15')
...
起初我认为它适合我的问题,但后来我意识到它需要在每个测试中指定。
我尝试使用 pytest_runtest_call 钩子来做到这一点,因此它会在每次测试运行中添加属性,而无需在每次测试中显式添加属性。但后来发现你不能在钩子中使用固定装置(这是有道理的)。
知道如何在不复制代码的情况下向 junit xml 输出文件添加属性吗?
编辑:我有另一个想法,那就是有一个装饰器来做到这一点。
def xml_decorator(test):
def runner(xml_record_attribute):
xml_record_attribute('index', '15')
test()
reutrn runner
我正在尝试将它与 pytest_collection_modifyitems 挂钩并装饰每个测试,但它不起作用。
def pytest_collection_modifyitems(session, config, items):
for item in items:
item.obj = xml_decorator(item.obj)
解决方案
您可以定义一个自动提供给每个测试用例的夹具:
import pytest
@pytest.fixture(autouse=True)
def record_index(record_xml_attribute):
record_xml_attribute('index', '15')
注意record_xml_attribute
xunit2 不支持,pytest v6 的默认 junit_family。要record_xml_attribute
与 pytest v6 一起使用,在pytest.ini
set
[pytest]
junit_family = xunit1
推荐阅读
- scala - 当列未知时,按特定值过滤 Spark Scala 数据框中的列
- python - Pygame.display.update() 不会在 while 循环的每次迭代中更新
- python - 这个 statsmodels 输出是什么意思?
- scala - 错误:scalac:加载规范时出错,Scala 签名规范的预期版本错误:发现 5.0:Specification.class 中的 4.1
- java - 如果用户输入 String 而不是 Int (JOptionPane),如何捕获错误
- python - Python - 1000 以下 3 或 5 的倍数之和
- python - 填充数据框列比 iterrows 更好的方法 - python + pandas
- javascript - 在css中,如何在保持其纵横比的同时最大化div内的图像大小?
- javascript - 导入 Handlebar 模板和获取它们之间的区别?
- macos - 如何将 isAlternative 与带有 customView 的 NSMenuItem 一起使用?