pytest文档8-html报告报错截图+失败重跑
前言
做web自动化的小伙伴应该都希望在html报告中展示失败后的截图,提升报告的档次,pytest-html也可以生成带截图的报告。
conftest.py
1.失败截图可以写到conftest.py文件里,这样用例运行时,只要检测到用例实例,就调用截图的方法,并且把截图存到html报告上
# conftest.py文件
# coding:utf-8
from selenium import webdriver
import pytest
driver = None
@pytest.mark.hookwrapper
def pytest_runtest_makereport(item):
"""
当测试失败的时候,自动截图,展示到html报告中
** 作者:上海-悠悠 QQ交流群:588402570**
:param item:
"""
pytest_html = item.config.pluginmanager.getplugin('html')
outcome = yield
report = outcome.get_result()
extra = getattr(report, 'extra', [])
if report.when == 'call' or report.when == "setup":
xfail = hasattr(report, 'wasxfail')
if (report.skipped and xfail) or (report.failed and not xfail):
file_name = report.nodeid.replace("::", "_")+".png"
screen_img = _capture_screenshot()
if file_name:
html = '<div><img src="data:image/png;base64,%s" alt="screenshot" style="width:600px;height:300px;" ' \
'onclick="window.open(this.src)" align="right"/></div>' % screen_img
extra.append(pytest_html.extras.html(html))
report.extra = extra
def _capture_screenshot():
'''
** 作者:上海-悠悠 QQ交流群:588402570**
截图保存为base64,展示到html中
:return:
'''
return driver.get_screenshot_as_base64()
@pytest.fixture(scope='session', autouse=True)
def browser(request):
global driver
if driver is None:
driver = webdriver.Firefox()
def end():
driver.quit()
request.addfinalizer(end)
return driver
2.用例部分如下:
# test_01.py文件
from selenium import webdriver
import time
#** 作者:上海-悠悠 QQ交流群:588402570**
def test_yoyo_01(browser):
browser.get("https://www.cnblogs.com/yoyoketang/")
time.sleep(2)
t = browser.title
assert t == "上海-悠悠"
# test_02.py文件
from selenium import webdriver
import time
# ** 作者:上海-悠悠 QQ交流群:588402570**
def test_yoyo_01(browser):
browser.get("https://www.cnblogs.com/yoyoketang/")
time.sleep(2)
t = browser.title
assert "上海-悠悠" in t
报告展示
1.cmd打开,cd到用例的目录,执行指令
$ pytest --html=report.html --self-contained-html

2.生成报告如下

失败重试
失败重跑需要依赖pytest-rerunfailures插件,使用pip安装就行
$ pip install pytest-rerunfailures

用例失败再重跑1次,命令行加个参数--reruns就行了
$ py.test --reruns 1 --html=report.html --self-contained-html

关于reruns参数的2个用法
re-run failing tests to eliminate flaky failures:
--reruns=RERUNS number of times to re-run failed tests. defaults to 0.
--reruns-delay=RERUNS_DELAY
add time (seconds) delay between reruns.
---------------------------------pytest结合selenium自动化完整版-------------------------
全书购买地址 https://yuedu.baidu.com/ebook/902224ab27fff705cc1755270722192e4536582b
作者:上海-悠悠 QQ交流群:874033608
也可以关注下我的个人公众号:yoyoketang

pytest文档8-html报告报错截图+失败重跑的更多相关文章
- pytest_html报告报错截图+失败重跑
前言 做web自动化的小伙伴应该都希望在html报告中展示失败后的截图,提升报告的档次,pytest-html也可以生成带截图的报告. conftest.py 1.失败截图可以写到conftest.p ...
- IE使用多彩文档上传数据库报错
使用多彩文档,用IE浏览器提交表单,双引号里面包含单引号,导致数据库插入不了,而用chrome浏览器不会报错,自动过滤单引号, 解决:content.replace("'", &q ...
- 使用java2Word生成Word文档打不开报错 存在非法字符xml
今天也不知道是该吐槽Java2word还是我的eclipse,总之就是使用Java2Word生成文档的时候文档生成没问题,但是生成的Word文档打不开还报错,存在非法字符xml,好扎心.终于找到了解决 ...
- C# 生成word 文档 代码 外加 IIS报错解决方案
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- selenium+python自动化81-html报告优化(饼图+失败重跑+兼容python2&3)
优化html报告 为了满足小伙伴的各种变态需求,为了装逼提升逼格,为了让报告更加高大上,测试报告做了以下优化: 测试报告中文显示,优化一些断言失败正文乱码问题 新增错误和失败截图,展示到html报告里 ...
- selenium+python自动化81-html报告优化(饼图+失败重跑+兼容python2&3)【转载】
优化html报告 为了满足小伙伴的各种变态需求,为了装逼提升逼格,为了让报告更加高大上,测试报告做了以下优化: 测试报告中文显示,优化一些断言失败正文乱码问题 新增错误和失败截图,展示到html报告里 ...
- pytest文档7-pytest-html生成html报告
前言 pytest-HTML是一个插件,pytest用于生成测试结果的HTML报告.兼容Python 2.7,3.6 pytest-html 1.github上源码地址[https://github. ...
- 【python-HTMLTestRunner】生成HTMLTestRunner报告报错ERROR 'ascii' codec can't decode byte 0xe5 in position 0: ordinal not in range(128)
[python-HTMLTestRunner]生成HTMLTestRunner报告报错:ERROR 'ascii' codec can't decode byte 0xe5 in position 0 ...
- pytest文档3-pycharm运行pytest
前言 上一篇pytest文档2-用例运行规则已经介绍了如何在cmd执行pytest用例,平常我们写代码在pycharm比较多 写完用例之后,需要调试看看,是不是能正常运行,如果每次跑去cmd执行,太麻 ...
随机推荐
- Linux下web服务的搭建
1.安装Apache Apache的官网地址为:http://httpd.apache.org/,这里以源码的方式进行安装,我们下载的版本是“httpd-2.4.25.tar.gz”,下载后的压缩文件 ...
- matplotlib 练习
官网 vamei的博客还是读了就秒懂,很妙, matplotlib核心剖析 官网翻译也不错,但缺少了 Logarithmic and other nonlinear axis对数等非线性轴 这一模块 ...
- Mutex 的正确打开方式
在使用 Mutex 在给线程/进程间加锁时,需要注意的问题. 1 AbandonedMutexException 在使用 mutex.WaitOne 时,可能抛出异常 AbandonedMutexEx ...
- 在phpWeChat中如何定义一个授权登录(获取昵称)的链接
在phpWeChat中如何定义一个授权登录(获取昵称)的超链接?使其点击后出现如下效果? 由于集成了这个功能,其实这个需要是很简单的. 假如您想在授权后跳转到http://www.baidu.com/ ...
- JSP中的9大内置对象四大域与servlet里的三大域
九大内置对象 隐式对象 说明 out 转译后对应JspWriter对象,其内部关联一个PringWriter对象 request 转译后对应HttpServletRequest/ServletRequ ...
- Linux-数据库1
数据库介绍 数据库(database,DB)是指长期存储在计算机内的,有组织,可共享的数据的集合.数据库中的数据按一定的数学模型组织.描述和存储,具有较小的冗余,较高的数据独立性和易扩展性,并可为各种 ...
- AFF镜像工具集afflib-tools
AFF镜像工具集afflib-tools Advanced Forensic Format(AFF)是一种开源免费的磁盘镜像格式.作为磁盘数字取证的三大格式之一,AFF提供数字取证的各项功能,如签 ...
- PHP函数声明(三)
/** * 一.任何参数的数量 * func_get_args()//接收一个数组,数组里面包含所有参数 * func_num_args()//取得共有几个参数 * func_get_arg(整数)/ ...
- POJ 3228 [并查集]
题目链接:[http://poj.org/problem?id=3228] 题意:给出n个村庄,每个村庄有金矿和仓库,然后给出m条边连接着这个村子.问题是把所有的金矿都移动到仓库里所要经过的路径的最大 ...
- 【BZOJ 1853】 1853: [Scoi2010]幸运数字 (容斥原理)
1853: [Scoi2010]幸运数字 Time Limit: 2 Sec Memory Limit: 64 MBSubmit: 2472 Solved: 911 Description 在中国 ...