我写了pytest测试结果自定义HTML报告巨蟒插件任意信息。我想在测试中存储一些任意的测试信息(i.o.一些python对象...),然后在制作报告时我想在报告中重复使用这些信息。到目前为止,我只是遇到了一些骇人听闻的解决方案。Pytest通过从测试
我将request
对象传递给我的测试,并用我的数据填充request.node._report_sections
部分。然后 这个对象传递给TestReport.sections
属性,它是通过挂钩pytest_runtest_logreport
可得到的,从中我终于可以生成HTML,然后我从sections
属性中删除我的所有对象。
在pseudopythoncode:
def test_answer(request):
a = MyObject("Wooo")
request.node._report_sections.append(("call","myobj",a))
assert False
和
def pytest_runtest_logreport(report):
if report.when=="call":
#generate html from report.sections content
#clean report.sections list from MyObject objects
#(Which by the way contains 2-tuples, i.e. ("myobj",a))
有没有更好的办法pytest做到这一点?