pytest中pytest-html生成html报告

先安装pip install pytest-html

指定报告路径

1.直接执行"pytest --html=report.html"生成的报告会在当前脚本的同一路径,如果想指定报告的存放位置,放到当前脚本的同一目录下的report文件夹里

pytest --html=./report/report.html 

Microsoft Windows [版本 6.1.7601]
版权所有 (c) 2009 Microsoft Corporation。保留所有权利。

C:\Users\Administrator>cd /d E:\PycharmProjects\api_pytest

E:\PycharmProjects\api_pytest>cd tests

E:\PycharmProjects\api_pytest\tests>pytest --html=./report/report.html
'pytest' 不是内部或外部命令,也不是可运行的程序
或批处理文件。

E:\PycharmProjects\api_pytest\tests>pytest --html=./report/report.html
'pytest' 不是内部或外部命令,也不是可运行的程序
或批处理文件。

E:\PycharmProjects\api_pytest\tests>pytest --html=./report/report.html
============================= test session starts =============================
platform win32 -- Python 3.7.1, pytest-6.1.1, py-1.9.0, pluggy-0.13.1
rootdir: E:\PycharmProjects\api_pytest, configfile: pytest.ini
plugins: allure-pytest-2.8.18, html-2.1.1, metadata-1.10.0
collected 16 items

test_fixturedemo1.py ...                                                 [ 18%]
test_fixturedemo2.py ...                                                 [ 37%]
test_fixturedemo3.py .....                                               [ 68%]
test_fixturedemo4.py ...                                                 [ 87%]
test_in_theaters.py .                                                    [ 93%]
test_simple.py .                                                         [100%]

- generated html file: file://E:\PycharmProjects\api_pytest\tests\report\report.
html -
============================= 16 passed in 1.15s ==============================

E:\PycharmProjects\api_pytest\tests>

生成报告的位置

 

报告独立显示

1.上面方法生成的报告,css是独立的,分享报告的时候样式会丢失,为了更好的分享发邮件展示报告,可以把css样式合并到html里

$ pytest --html=report.html --self-contained-html

 

Pytest-html 报告中文标题乱码

公司有个自己搭建的UI自动化测试项目,使用python3.7 + pytest + selenium + Chrome搭建而成的。
为了方便Case的标识,我个人把Case的文件起名为“def test_你好”这种格式,在调试时中文显示正常,但是在pytest-html生成的 report.html 报告中,HTML报告中对应test一栏中文显示成乱码了,如“test_公开å”这种形式的,但整个报告除了此处,其他位置中文均显示正常,研究了很多方法,网上各种方法都尝试了,包括代码调试,指定utf-8编码,对字符串各种编解码,均无效。

后来有幸看到了https://blog.csdn.net/wang785994599/article/details/97653329 这篇博文,而后对比pytest-html中的源码,终于找到了问题的所在

在pytest-html 插件下的plugin.py中(路径:/lib/python3.7/site-packages/pytest_html/plugin.py),对于test_id转换出现了问题,按照上述博文的描述中显示的那样,当content=‘你好’(中文或中英文混杂时)
content = content.encode(“utf-8”).decode(“unicode-escape”)
==> /哈哈 中文被转码导致乱码
根据博主给出的解决方法,修改plugin.py中对于test_id的编码赋值
self.test_id = re.sub(r’(\u[\s\S]{4})’, lambda x: x.group(1).encode(“utf-8”).decode(“unicode-escape”), report.nodeid)
最终生成的报告中显示正确了。

 

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值