Pytest—HTML报告修改

conftest.py 内做修改

 

 1 import pytest
 2 from py._xmlgen import html
 3 '''
 4 pytest里面默认读取conftest.py里面的配置
 5 
 6 
 7 conftest.py配置需要注意以下点:
 8     conftest.py配置脚本名称是固定的,不能改名称
 9     conftest.py与运行的用例要在同一个pakage下,并且有__init__.py文件
10     不需要import导入 conftest.py,pytest用例会自动查找
11 '''
12 # def pytest_html_report_title(report):
13 #     report.title = "My very own title!"
14 
15 
16 
17 
18 '''Environment 修改
19     pytest-metadata提供Environment section,
20     通过pytest_configure访问 '''
21 def pytest_configure(config):
22     # 添加接口地址与项目名称
23     config._metadata["项目名称"] = "SuperTest v1.0"
24     config._metadata['接口地址'] = 'https://www.baidu.com/'
25     # 删除Java_Home
26     config._metadata.pop("JAVA_HOME")
27 
28 
29 '''Additional summary information  
30     附加摘要信息'''
31 def pytest_html_results_summary(prefix, summary, postfix):
32     prefix.extend([html.p("所属部门: 帝国测试中心")])
33     prefix.extend([html.p("负责人: 蒙奇·D·路飞")])
34 
35 
36 
37 
38 '''在报表对象上创建'额外'列表,从而向HTML报告添加详细信息'''
39 @pytest.hookimpl(hookwrapper=True)
40 def pytest_runtest_makereport(item, call):
41     pytest_html = item.config.pluginmanager.getplugin('html')
42     outcome = yield
43     report = outcome.get_result()
44     extra = getattr(report, 'extra', [])
45     if report.when == 'call':
46         # always add url to report
47         extra.append(pytest_html.extras.url('http://www.baidu.com/'))
48         xfail = hasattr(report, 'wasxfail')
49         if (report.skipped and xfail) or (report.failed and not xfail):
50             # only add additional html on failure
51             extra.append(pytest_html.extras.html('<div>Additional HTML</div>'))
52         report.extra = extra

 

posted @ 2020-08-05 12:00  昨日不可追  阅读(460)  评论(0编辑  收藏  举报