我寫了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做到這一點?