51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

手机号码,快捷登录

查看: 636|回复: 0
打印 上一主题 下一主题

[资料] 想用Pytest生成一份无懈可击的测试报告吗?

[复制链接]
  • TA的每日心情
    擦汗
    2024-5-31 09:15
  • 签到天数: 44 天

    连续签到: 1 天

    [LV.5]测试团长

    跳转到指定楼层
    1#
    发表于 2022-8-18 14:59:29 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    安装插件
    要生成HTML类型的报告,需要使用pytest-html插件,可以在IDE中安装,也可以在命令行中安装。插件安装的位置涉及到不同项目的使用,这里不再详述,想了解的可自行查询。
    IDE中安装
    1. <font size="3">在File>Settings>;Project>;Project Interpreter界面,点击“ + ”搜索pytest-html即可进行安装。</font>
    复制代码

    命令行安装

    建议先在命令行中切换到Python安装路径“ Lib\site-packages ”目录,再执行安装命令。

    1. <font size="3">pip install -U pytest-html</font>
    复制代码

    (左右滑动查看完整代码)

    生成html报告

    先准备一个简单的执行脚本:

    1. <font size="3">import pytest


    2. def fun(x):
    3.     return x + 1


    4. def test_answer_1():
    5.     """测试断言一"""
    6.     assert fun(3) == 4
    7. def test_answer_2():
    8.     """测试断言二"""
    9.     assert fun(5) == 7


    10. @pytest.mark.parametrize("test_input,expected",[
    11.     ("3+5",8),
    12.     ("2+4",6),
    13.     pytest.param("6 * 9",42,marks=pytest.mark.xfail),
    14.     pytest.param("6 * 6",42,marks=pytest.mark.skip)
    15. ])


    16. def test_mark(test_input,expected):
    17.     """用例集合"""
    18.     assert eval(test_input) == expected


    19. if __name__ == '__main__':
    20.     pytest.main(['-v','--html=report.html','test_08.py'])</font>
    复制代码

    (左右滑动查看完整代码)

    生成报告命令 pytest --html=报告名称 要执行的脚本文件  ,执行上述脚本查看结果。

    report.html:报告名称,记录报告生成时间以及插件版本;

    Environment:测试环境;

    Summary:用例统计;

    Results:测试结果,点击Show all details / Hide all details可以展开结果详情或收缩全部结果。

    使用小技巧

    指定路径

    通过上述命令运行脚本后可以发现,测试报告保存在项目的根目录下,查找报告比较繁琐。

    我们可以在运行命令中指定报告路径 pytest -v --html=./outputs/report.html test_08.py ,代码执行完成,可以发现项目根目录下生成了outputs文件,测试报告也在其中。

    报告独立

    当本地执行完成,想把测试报告分享出去,却发现分享出去的报告打开后样式丢失。

    因为代码执行完成会生成assets文件,将CSS保存在了本地。我们可以通过命令将CSS写入HTML中,这样生成的测试报告就能对外分享了。

    1. <font size="3">pytest -v --html=./outputs/report.html --self-contained-html test_08.py</font>
    复制代码

    (左右滑动查看完整代码)

    报告优化

    在实际的工作中,通过上述操作生成的测试报告一般不是我们想要的结果。

    环境信息通过增减更换成需要展示的内容、增加用例描述、去掉多余的列等等。这里需要将优化代码写入conftest.py文件,该文件名是固定的不可更改。

    导入引用包:

    1. <font size="3">import pytest
    2. from py._xmlgen import html
    3. from datetime import datetime</font>
    复制代码

    (左右滑动查看完整代码)


    修改测试环境:

    1. <font size="3">@pytest.mark.parametrize
    2. def pytest_configure(config):
    3.     config._metadata.pop("JAVA_HOME") # 删除java_home
    4.     config._metadata["项目名称"] = "引擎自动化" # 添加项目名称
    5.     config._metadata["接口地址"] = "https://www.example.com/poke" # 添加接口地址</font>
    复制代码

    (左右滑动查看完整代码)


    修改用例统计:

    1. <font size="3">@pytest.mark.parametrize
    2. def pytest_html_results_summary(prefix,summary,postfix):
    3.     prefix.extend([html.p("所属部门:测试组")])
    4.     prefix.extend([html.p("测试人员:许卫玲")])</font>
    复制代码

    (左右滑动查看完整代码)


    修改结果显示:

    1. <font size="3">@pytest.mark.optionalhook
    2. def pytest_html_results_table_header(cells):
    3.     cells.insert(1,html.th("Description")) # 表头添加Description
    4.     cells.insert(2,html.th("Time",class_="sortable time",col="time"))
    5.     cells.pop(-1) # 删除link


    6. @pytest.mark.optionalhook
    7. def pytest_html_results_table_row(report,cells):
    8.     cells.insert(1,html.td(report.description)) # 表头对应的内容
    9.     cells.insert(2,html.td(datetime.now(),class_="col-time"))
    10.     cells.pop(-1) # 删除link


    11. @pytest.mark.hookwrapper
    12. def pytest_runtest_makereport(item,call): # Description取值为用例说明__doc__
    13.     outcome = yield
    14.     report = outcome.get_result()
    15.     report.description = str(item.function.__doc__)
    16.     report.nodeid = report.nodeid.encode("utf-8").decode("unicode_escape")</font>
    复制代码

    (左右滑动查看完整代码)


    修改完成,重新执行脚本,查看最终效果。






    本帖子中包含更多资源

    您需要 登录 才可以下载或查看,没有帐号?(注-册)加入51Testing

    x
    分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

    本版积分规则

    关闭

    站长推荐上一条 /2 下一条

    小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

    GMT+8, 2024-6-8 04:59 , Processed in 0.071825 second(s), 24 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

    快速回复 返回顶部 返回列表