前言
httprunner 可以自定義生成測試報告的模板,1.x版本里面有個 extent_report_template.html 模塊非常美觀。
但是生成報告的時候會報錯:jinja2.exceptions.UndefinedError: 'dict object' has no attribute 'meta_data'
環境:httprunner==2.4.3
遇到問題
httprunner 2.x 版本已經去掉 extent_report_template.html 了,所以在\Lib\site-packages\httprunner\templates 下找不到。
可以把1.x 版本里面的模塊復制出來,在項目跟目錄下新建一個 report目錄,再新建 report/template 目錄。
運行 testsuites 目錄下用例
hrun testsuites --report-template reports/template/extent_report_template.html
運行后出現報錯: jinja2.exceptions.UndefinedError: 'dict object' has no attribute 'meta_data'
!!!!!!!!!! exception stage: generate html report !!!!!!!!!!
Traceback (most recent call last):
File "E:\python36\lib\runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "E:\python36\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "D:\soft\venv_hrun\Scripts\hrun.exe\__main__.py", line 7, in <module>
File "d:\soft\venv_hrun\lib\site-packages\httprunner\cli.py", line 101, in main
report_file=args.report_file
File "d:\soft\venv_hrun\lib\site-packages\httprunner\report.py", line 321, in gen_html_report
).render(summary)
File "d:\soft\venv_hrun\lib\site-packages\jinja2\environment.py", line 1090, in render
self.environment.handle_exception()
File "d:\soft\venv_hrun\lib\site-packages\jinja2\environment.py", line 832, in handle_exception
reraise(*rewrite_traceback_stack(source=source))
File "d:\soft\venv_hrun\lib\site-packages\jinja2\_compat.py", line 28, in reraise
raise value.with_traceback(tb)
File "<template>", line 264, in top-level template code
File "d:\soft\venv_hrun\lib\site-packages\jinja2\environment.py", line 471, in getattr
return getattr(obj, attribute)
jinja2.exceptions.UndefinedError: 'dict object' has no attribute 'meta_data'
這個主要是 2.x 版本的生成的報告跟 1.x 不一樣導致的。
解決辦法
先找個適配2.x 版本的模板, 我試了下目前最高只能適配到 httprunner2.4.3 版本, 其他高點的版本不適配了。
先降級 之前的httrunner2.5.7 版本,改成 httprunner2.4.3
pip install httprunner==2.4.3 --index-url https://pypi.douban.com/simple
版本降級后,把新的模板放到 report/template 目錄,為了跟之前的區分開,這里模板名稱改成 extent_report_template2.4.3.html
再次運行用例
hrun testsuites --report-template reports/template/extent_report_template2.4.3.html
最終生成報告效果
下載地址
接下來小伙伴門最關心的是 extent_report_template2.4.3.html 在哪下載的問題了!
第一步先關注微信公眾號:yoyoketang
回復:template
領取下載地址!
2.5.7版本在github下載https://github.com/FuckyouBB/extent_report_template_httprunner2