pytest 失败重跑截图
1.环境准备
/*@param:
作者:流浪的python
Date:2019/01/19
env:python 3.7(由于3.0-3.5以下部分pytest可能有部分兼容问题安装建议2.7-2.9,3.5-最新)
pip install pytest专属 pytest框架包
pip install pytest-html pytest自己专属报告包
pip install pytest-rerunfailures 失败重跑包也是pytest专属
并发的也可以安下,利用多cpu运行调高用例执行速度
python -m pip install xdist
2.用例配置conftest.py编写,这个文件名必须是这个,官网指定的,pytest运行前会自己寻找这个文件,如果有会按里面配置跑没有就默认按用例设置跑,conftest.py仅对同目录,同目录下文件生效,超出按自己的conftest.py配置,每个包可以有自己的conftest.py
目录结构:https://docs.pytest.org/en/latest/pythonpath.html#test-modules-conftest-py-files-inside-packages
参考以上链接官网解释
这里我给出我的结构供参考:
project/
|- __init__.py
|- conftest.py
|- bar/
|- __init__.py
|- tests/
|- __init__.py
|- test_foo.py
conftest.py
# coding=utf-8
from selenium import webdriver
import pytest
import os
driver = None
@pytest.mark.hookwrapper
def pytest_runtest_makereport(item):
"""
Extends the PyTest Plugin to take and embed screenshot in html report, whenever test fails.
: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):
dirpng=r'./report/png/'
if os.path.exists(dirpng) and os.path.isdir(dirpng):
pass
else:
os.mkdir(dirpng)
file_name = dirpng + report.nodeid.replace("::", "_")+".png"
file_name1=r'./png/'+ report.nodeid.replace("::", "_")+".png"
_capture_screenshot(file_name)
if file_name:
html = '<div><img src="%s" alt="screenshot" style="width:304px;height:228px;" ' \
'onclick="window.open(this.src)" align="right"/></div>' % file_name1
extra.append(pytest_html.extras.html(html))
report.extra = extra
def _capture_screenshot(name):
driver.get_screenshot_as_file(name)
@pytest.fixture(scope='session', autouse=True)
def browser():
global driver
if driver is None:
driver = webdriver.Chrome()
return driver
3例脚本编写
# coding=utf-8
import pytest
class TestFailScreenShoot(object):
def test_run001(self):
assert 1==2
def test_run002(self):
assert 1==1
def test_screenshot_on_test_failure(self,browser):
browser.get("https://www.baidu.com")
assert False
# if __name__ == "__main__":
# pytest.main('-s','-q')
4.cmd 到用例所在目录,Tests目录retry次数2,失败重跑延迟间隔2秒
输入: py.test --html=./report/test_report.html -v --reruns 2 --reruns-delay 2 看到cmd提示如下:
generated html file: C:\Users\Administrator\PycharmProjects\pytestframe\foo\bar\Tests\report\test_report.html
根据提示浏览器地址栏输入file:///C:/Users/Administrator/PycharmProjects/pytestframe/foo/bar/Tests/report/test_report.html


pytest 失败重跑截图的更多相关文章
- pytest失败重跑
一.说明 平常在做功能测试的时候,经常会遇到某个模块不稳定,偶然会出现一些bug,对于这种问题我们会针对此用例反复执行多次,最终复现出问题来.自动化运行用例时候,也会出现偶然的bug,可以针对单个用例 ...
- pytest文档8-html报告报错截图+失败重跑
前言 做web自动化的小伙伴应该都希望在html报告中展示失败后的截图,提升报告的档次,pytest-html也可以生成带截图的报告. conftest.py 1.失败截图可以写到conftest.p ...
- testng优化:失败重跑,extentReport+appium用例失败截图,测试报告发邮件
生成的单html方便jenkins集成发邮件,= = 构建失败发邮件 参考:https://blog.csdn.net/galen2016/article/details/77975965 步骤: 1 ...
- python3 unittest框架失败重跑加截图支持python2,python3
github源码地址下载:https://github.com/GoverSky/HTMLTestRunner_cn.git 解压文件后取出/HTMLTestRunner_cn.py文件丢进C:\Py ...
- testng增加失败重跑机制
注: 以下内容引自 http://www.yeetrack.com/?p=1015 testng增加失败重跑机制 Posted on 2014 年 10 月 31 日 使用Testng框架搭建自动测试 ...
- 【转载】扩展Robot Framework,实现失败用例自动再执行(失败重跑)
使用自动化脚本进行测试,经常受环境影响等各方面导致本能成功的脚本失败,下面介绍了RFS框架下,失败重跑的方法: 通过改写RobotFramework源代码增加--retry选项,实现test级别的失败 ...
- RF实现多次失败重跑结果合并的基础方法和优化方法
实现思路:通过分次执行失败案例重跑,然后通过结果文件合并命令实现多次失败重跑结果文件的合并,并输出合并后的log和report文件: 说明:具体失败案例重跑命令和结果文件合并命令请参考本博客其他相关章 ...
- testng失败重跑
重跑失败场景 1.要添加两个文件 背景:因为这里只是想单独展示失败的重跑的案例,所以先暂时把app这块的运行注释掉,只跑一个简单的demo,就一个简单类,类中就3个测试方法,失败重跑的原理是,运行方法 ...
- TestNg失败重跑—解决使用 dataProvider 参数化用例次数冲突问题
问题背景 在使用 testng 执行 UI 自动化用例时,由于 UI自动化的不稳定性,我们在测试的时候,往往会加上失败重跑机制.在不使用 @DataProvider 提供用例参数化时,是不会有什么问题 ...
随机推荐
- Review——JS的异步与同步
一.概念 同步(synchronous):指在js的主线程上,所有任务被依次执行: 异步(asynchronous):指任务不进入主线程,进入任务队列(task):当“任务队列”通知主线程,异步任务才 ...
- PHP IN_ARRAY 函数 使用需要注意的地方
今天 看PPChttp://bbs.phpchina.com/thread-171993-1-7.html 这个问题. 其实关键还是因为 php是弱类型语言,php进行比较的时候 最好还是使用stri ...
- BZOJ1911: [Apio2010]特别行动队(dp 斜率优化)
题意 题目链接 Sol 裸的斜率优化,注意推导过程中的符号问题. #include<bits/stdc++.h> #define Pair pair<int, int> #de ...
- gulp实用配置(1)——demo
在React和Vue推进下,现在很多人都在使用webpack作为自动化构建工具,但其实在很多时候我们并不是一定需要用到它,gulp这样的轻量级构建工具就足够了. 最近一段时间不是太忙,所以就写了三份配 ...
- 移动端点击输入框,弹出键盘,底部被顶起问题(vue)
这个问题相信做移动端开发的童鞋会有深刻体会,以前用jq开发时就很头疼这个问题,每次底部footer部分需要用position:fixed,如果页面内容不是很长,没有超出屏幕范围,那就还好,没有问题:一 ...
- 浏览器根对象window之caches
在控制台输入window. caches可输出一个叫做CacheStorage的对象,它用来存储 Response 对象的.也就是说用来对 HTTP ,响应做缓存的.虽然 localStorage 也 ...
- opencv3.2.0图像离散傅里叶变换
源码: ##名称:离散傅里叶变换 ##平台:QT5.7.1+opencv3.2.0 ##日期:2017年12月13. /**** 新建QT控制台程序****/ #include <QCoreAp ...
- 润乾报表一个页面中的echarts地图与其他区块的联动
需求概述: DBD样式效果如下图所示,需要点击左侧地图中的地区,右侧的仪表盘,柱线图可以对应显示对应该地区的数据. 实现思路: 分别制作带有地图.仪表盘.柱线图的3张报表:将3张报表放到DBD中设置布 ...
- 【Kettle】1、简单介绍
本人为转载,感谢作者的辛勤劳作:http://blog.csdn.net/rotkang/article/details/20810921 ------------------------------ ...
- [SQLServer] 内存占用查看
SELECT (physical_memory_in_use_kb/1024) AS Memory_usedby_Sqlserver_MB, (locked_page_allocations_kb/1 ...