关于如何自定义修改pytest-html报告深度学习总结

第一、pytest-html执行命令总结:

pytest test_case.py --html=report.html --self-contained-html 直接html独立显示
pytest test_case.py --html=report.html 包含assets样式文件,htmL显示需要依赖于此文件

 

 

第二、关于如何修改pytest-html的报告内容:
1、如何修改report.html中的Environment部分,可在conftest.py中实现:

def pytest_configure(config):
    config._metadata['测试地址'] = '192.168.50.XXX'

报告结果变化:

 

 

2、如何修改report.html中的Summary部分,可在conftest.py中实现:

import pytest
from py._xmlgen import html

@pytest.mark.optionalhook
def pytest_html_results_summary(prefix, summary, postfix):
    prefix.extend([html.p("测试人: 龙雄")])

报告结果变化:

 

 

3、如何修改report.html中的results-table部分,可在conftest.py中实现:

from datetime import datetime
import pytest

@pytest.mark.optionalhook
def pytest_html_results_table_header(cells):
    cells.insert(2, html.th('Description'))
    cells.insert(3, html.th('Time', class_='sortable time', col='time'))
    # cells.insert(1,html.th("Test_nodeid"))
    cells.pop()

@pytest.mark.optionalhook
def pytest_html_results_table_row(report, cells):
    cells.insert(2, html.td(report.description))
    cells.insert(3, html.td(datetime.utcnow(), class_='col-time'))
    # cells.insert(1,html.td(report.nodeid))
    cells.pop()

@pytest.mark.hookwrapper
def pytest_runtest_makereport(item, call):
    outcome = yield
    report = outcome.get_result()
    report.description = str(item.function.__doc__)
    report.nodeid = report.nodeid.encode("utf-8").decode("unicode_escape")   #设置编码显示中文

报告结果变化:

注意:如果测试用例中,传递的@pytest.mark.parametrize中的参数是列表中带字典的方式,那么报告显示的结果将为:

若传递的@pytest.mark.parametrize中的参数是列表中带元祖或列表的方式(如: bdata = [(1, "升级专业版本"),(2, "官方网站")];bdata=[[1,2],[3,4]]),那么报告显示的结果将为:

 

 

 

所以本人之前一直不知道如何将我的这些测试数据展示到report table中,因为本人之前一直用的是字典的方式,今天发现原来传递的数据类型会影响报告的展示内容,不知是否还有其他更好的解决方式,

有的话请大家赐教

 

所以本人的解决方案是,将传递的列表数组存储为元祖的方式:

import pytest
from dotest import mytest
import json
import logging
import requests

bdata = mytest()
s = requests.session()
logging.getLogger().setLevel(logging.INFO)

lb = []
for i in bdata:
    print(i)
    print(i['url'])
    yz = (i,i['url'],i['casename'])
    lb.append(yz)

@pytest.mark.parametrize("bdata,f,m", lb)
def testsingle(bdata,f,m):
    '''登录模块'''
    ip = bdata['ip']
    url = bdata['url']
    headers = json.loads(bdata['headers'])
    checkpoint = bdata['checkpoint']
    method = bdata['method']
    casename = bdata['casename']
    if bdata['datatype']=='json':
        body = json.loads(bdata['body'])
        result = s.request(method=method,url=ip+url,headers=headers,json=body)
        r = json.loads(result.text)
        assert checkpoint in r['message']
        logging.info("pass     "+casename  +"  "+url)

 


更多参考文章推荐:
https://www.cnblogs.com/linuxchao/p/linuxchao-pytest-html.html

posted @ 2019-02-18 15:25  乐乐熊小妹  阅读(4173)  评论(0编辑  收藏  举报