pytest学习(四)

自定义报告内容

from time import strftime
import pytest
from py._xmlgen import html


@pytest.mark.optionalhook
def pytest_html_results_table_header(cells):
    cells.insert(1, html.th('用例描述'))
    # 删除原第三列表头
    cells.pop(2)
    cells.insert(2, html.th('用例'))
    cells.insert(3, html.th('操作时间', class_='sortable time', col='time'))
    # cells.insert(1,html.th("Test_nodeid"))
    cells.pop()
    cells.pop(-1)


@pytest.mark.optionalhook
def pytest_html_results_table_row(report, cells):
    cells.insert(1, html.td(report.description))
    # 删除原第三列数据
    cells.pop(2)
    cells.insert(2, html.td(report.nodeid))
    cells.insert(3, html.td(strftime('%Y-%m-%d %H:%M:%S'), class_='col-time'))
    # cells.insert(1,html.td(report.nodeid))
    cells.pop()
    cells.pop(-1)


@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.optionalhook
def pytest_html_report_title(report):
    report.title = "接口测试报告"


# 修改Environment部分信息,配置测试报告环境信息
def pytest_configure(config):
    # 添加接口地址与项目名称
    config._metadata["项目名称"] = "Flychord琴行端V2.2.4"
    config._metadata['接口地址'] = 'http://api.XXXX.XXXX.com'
    config._metadata['开始时间'] = strftime('%Y-%m-%d %H:%M:%S')
    # 删除Java_Home
    config._metadata.pop("JAVA_HOME")
    config._metadata.pop("Packages")
    config._metadata.pop("Platform")
    config._metadata.pop("Plugins")
    config._metadata.pop("Python")


# 修改Summary部分的信息
def pytest_html_results_summary(prefix, summary, postfix):
    prefix.extend([html.p("所属部门: 技术部")])
    prefix.extend([html.p("测试人: 大帅哥")])

汉化测试报告

如果是在pycharm中下载的插件:
进入C:\Users\Michelle\AppData\Roaming\Python\Python37\site-packages中找到pytest_html替换成我这里的pytest_html(文件是别人写好的)

如果是使用pip下载的插件:
进入C:\Program Files\Python37\Lib\site-packages中找到pytest_html替换成我这里的pytest_html(文件是别人写好的)

也可以自己修改代码:
由于本人较懒,所以直接拿别人已改好的代码,以下是别人的内容
在这里插入图片描述
修改plugin.py:

# if len(log) == 0:
#     log = html.div(class_='empty log')
#     log.append('No log output captured.')
if len(log) == 0:  # modify by linux超
       log = html.div(class_='empty log')
       log.append('未捕获到日志')
additional_html.append(log)
# head = html.head(
#     html.meta(charset='utf-8'),
#     html.title('Test Report'),
#     html_css)
head = html.head(  # modify by linux超
       html.meta(charset='utf-8'),
       html.title('测试报告'),
       html_css)
# outcomes = [Outcome('passed', self.passed),
#             Outcome('skipped', self.skipped),
#             Outcome('failed', self.failed),
#             Outcome('error', self.errors, label='errors'),
#             Outcome('xfailed', self.xfailed,
#                     label='expected failures'),
#             Outcome('xpassed', self.xpassed,
#                     label='unexpected passes')]
outcomes = [Outcome('passed', self.passed, label="通过"),
            Outcome('skipped', self.skipped, label="跳过"),
            Outcome('failed', self.failed, label="失败"),
            Outcome('error', self.errors, label='错误'),
            Outcome('xfailed', self.xfailed,
                    label='预期失败'),
            Outcome('xpassed', self.xpassed,
                    label='预期通过')]
# if self.rerun is not None:
#     outcomes.append(Outcome('rerun', self.rerun))
if self.rerun is not None:
    outcomes.append(Outcome('重跑', self.rerun))

# summary = [html.p(
#     '{0} tests ran in {1:.2f} seconds. '.format(
#         numtests, suite_time_delta)),
#     html.p('sfsf',
#            class_='filter',
#            hidden='true')]
summary = [html.p(  # modify by linux超
    '执行了{0}个测试用例, 历时:{1:.2f}秒 . '.format(
        numtests, suite_time_delta)),
    html.p('(取消)勾选复选框, 以便筛选测试结果',
           class_='filter',
           hidden='true')]
# cells = [
#     html.th('Result',
#             class_='sortable result initial-sort',
#             col='result'),
#     html.th('Test', class_='sortable', col='name'),
#     html.th('Duration', class_='sortable numeric', col='duration'),
#     html.th('Links')]# modify by linux超
cells = [
    html.th('通过/失败',
            class_='sortable result initial-sort',
            col='result'),
    html.th('用例', class_='sortable', col='name'),
    html.th('耗时', class_='sortable numeric', col='duration'),
    html.th('链接')]
# results = [html.h2('Results'), html.table([html.thead(
#     html.tr(cells),
#     html.tr([
#         html.th('No results found. Try to check the filters',
#                 colspan=len(cells))],
#             id='not-found-message', hidden='true'),
#     id='results-table-head'),
#     self.test_logs], id='results-table')]
results = [html.h2('测试结果'), html.table([html.thead(  # modify by linux超
    html.tr(cells),
    html.tr([
        html.th('无测试结果, 试着选择其他测试结果条件',
                colspan=len(cells))],
            id='not-found-message', hidden='true'),
    id='results-table-head'),
    self.test_logs], id='results-table')]
#html.p('Report generated on {0} at {1} by '.format( 
html.p('生成报告时间{0} {1} Pytest-Html版本:'.format(  # modify by linux超
# body.extend([html.h2('Summary')] + summary_prefix
#             + summary + summary_postfix)
body.extend([html.h2('用例统计')] + summary_prefix  # modify by linux超
            + summary + summary_postfix)
# environment = [html.h2('Environment')]
environment = [html.h2('测试环境')]  # modify by linux超

修改main.js:

/*
function add_collapse() {
    // Add links for show/hide all
    var resulttable = find('table#results-table');
    var showhideall = document.createElement("p");
    showhideall.innerHTML = '<a href="javascript:show_all_extras()">Show all details</a> / ' +
                            '<a href="javascript:hide_all_extras()">Hide all details</a>';
    resulttable.parentElement.insertBefore(showhideall, resulttable);
*/
function add_collapse() {  // modify by linux超
    // Add links for show/hide all
    var resulttable = find('table#results-table');
    var showhideall = document.createElement("p");
    showhideall.innerHTML = '<a href="javascript:show_all_extras()">显示详情</a> / ' +
                            '<a href="javascript:hide_all_extras()">隐藏详情</a>';
    resulttable.parentElement.insertBefore(showhideall, resulttable);

修改style.css:

.expander::after {
    content: " (展开详情)";
    color: #BBB;
    font-style: italic;
    cursor: pointer;
}
.collapser::after {
    content: " (隐藏详情)";
    color: #BBB;
    font-style: italic;
    cursor: pointer;
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值