【pytest-17】pytest-html添加描述信息
前言
pytest-html测试报告默认是不展示用例描述Description内容,可以修改生成的报告内容的,可以自己添加和删除html报告的table内容。
先上效果图
修改报告
参考官方文档地址:https://pypi.org/project/pytest-html/
新建conftest.py文件
mport pytest from py.xml import html import pytest from datetime import datetime @pytest.hookimpl(hookwrapper=True) def pytest_runtest_makereport(item, call): pytest_html = item.config.pluginmanager.getplugin("html") outcome = yield report = outcome.get_result() extra = getattr(report, "extra", []) if report.when == "call" or report.when=="setup": # always add url to report extra.append(pytest_html.extras.url("http://www.example.com/")) xfail = hasattr(report, "wasxfail") if (report.skipped and xfail) or (report.failed and not xfail): # only add additional html on failure extra.append(pytest_html.extras.html("<div>Additional HTML</div>")) report.extra = extra report.description = str(item.function.__doc__)#添加用例描述 """ Environment部分 """ def pytest_configure(config): config._metadata['测试地址'] = "localhost:63342" # """ Summary部分 """ @pytest.mark.optionalhook def pytest_html_results_summary(prefix, summary, postfix): #Get configure content. prefix.extend([html.p("测试人: 测试组")])
""" Results部分-头 """ def pytest_html_results_table_header(cells): cells.insert(1, html.th("Description")) cells.insert(3, html.th("Time", class_="sortable time", col="time")) cells.pop() """ Results部分-行 """ def pytest_html_results_table_row(report, cells): cells.insert(1, html.td(report.description)) cells.insert(3, html.td(datetime.now(), class_="col-time"))#当前时间 cells.pop()
更改描述部分
pytest-html默认获取的是测试方法的__doc__属性,也就是,测试函数下的注释 如下的""" """中的内容.
test_a.py文件:
#encoding:utf-8 #@Time:2020/12/14 14:24 #@Author:sunny import pytest def test_hello(): '''test_hello测试用例''' print("hello world") @pytest.mark.xfail() def test_1(): '''test_1测试用例''' a="hello" b="hello world" assert a==b @pytest.mark.xfail() def test_2(): '''test_2测试用例''' a="hello" b="hello world" assert a !=b if __name__ == '__main__': pytest.main(["-s","test_xpass.py"])
善于跌倒仍喜爱奔跑~

修改报告
浙公网安备 33010602011771号