python pytest测试框架介绍四----pytest-html插件html带错误截图及失败重测机制
一、html报告错误截图
这次介绍pytest第三方插件pytest-html
这里不介绍怎么使用,因为怎么使用网上已经很多了,这里给个地址给大家参考,pytest-html生成html报告
今天在这里介绍pytest生成的报告怎么带有截图,这在web自动化测试非常有用。
需求是测试用例错误就截图,方法如下:
我们要新建一个关于截图的插件文件conftest.py,注意,文件名不能变,因为pytest-html会自动找这个自己写的插件,内容如下:
from selenium import webdriver
import pytest
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):
file_name = 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_name
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.Firefox()
return driver
关于conftest.py文件怎么应用,可以查看文档:conftest.py how to put
接下来,就是写用例了,在与conftest当前文件夹下写用例文件test_aa.py,如下
def test_screenshot_on_test_failure(browser):
browser.get("https://www.baidu.com")
assert False
然后再次用pytest运行,运行方式如下:
E:\>pytest -s -v test_aa.py --html=report.html
然后我们可以在E盘下看到生成了report.html文件及测试用例为名的png截图文件
打开html文件,详情如下:

参考:
https://pypi.python.org/pypi/pytest-html
二、失败重试
使用的插件是pytest-rerunfailures,官网这里
使用方法:
在测试时加入--rerun参数
py.test --rerun 2 用例失败再重测2次
python pytest测试框架介绍四----pytest-html插件html带错误截图及失败重测机制的更多相关文章
- 『德不孤』Pytest框架 — 1、Pytest测试框架介绍
目录 1.什么是单元测试框架 2.单元测试框架主要做什么 3.单元测试框架和自动化测试框架有什么关系 4.Pytest测试框架说明 5.Pytest框架和Unittest框架区别 (1)Unittes ...
- 【pytest系列】- pytest测试框架介绍与运行
如果想从头学起pytest,可以去看看这个系列的文章! https://www.cnblogs.com/miki-peng/category/1960108.html 前言 目前有两种纯测试的测 ...
- python pytest测试框架介绍二
在介绍一中简单介绍了pytest的安装和简单使用,接下来我们就要实际了解pytest了 一.pytest的用例发现规则 pytest可以在不同的函数.包中发现用例,发现的规则如下 文件名以test_开 ...
- python pytest测试框架介绍三
之前介绍了pytest以xUnit形式来写用例,下面来介绍pytest特有的方式来写用例 1.pytest fixture实例1 代码如下 from __future__ import print_f ...
- python pytest测试框架介绍一
一.安装 pytest不是python默认的package,需要自动手工安装. pytest支持python 2.6--3.5之间的版本,同时可以在unix及windows上安装 安装方式: pip ...
- python pytest测试框架介绍五---日志实时输出
同样的,在使用pytest进行自动化测试时,需要将实时日志打印出来,而不是跑完后才在报告中出结果. 不过,好在pytest在3.3版本开始,就支持这一功能了,而不用再像nose一样,再去装第三方插件. ...
- pytest测试框架+jenkins结合pytest+jenkins邮件通知配置
刚刚做完一个项目,由于这是一个方案项目,而不是产品,所以各种准备很不充分,很多公司的能力不能复用,整个团队又都是新员工,而且有部分实习生,匆忙上马,今天对我的自动化框架做一个回溯 自动化测试框架的选择 ...
- pytest测试框架 -- 简介
一.pytest测试框架简介: (1)pytest是python的第三方测试框架,是基于unittest的扩展框架,比unittest更简洁,更高效. (2)pytest框架可以兼容unittest用 ...
- Pytest测试框架(五):pytest + allure生成测试报告
Allure 是一款轻量级.支持多语言的开源自动化测试报告生成框架,由Java语言开发,可以集成到 Jenkins. pytest 测试框架支持Allure 报告生成. pytest也可以生成juni ...
随机推荐
- Backlight当前行背景高亮显示
下载地址:https://github.com/limejelly/Backlight-for-XCode PS:Xcode 8.0 默认支持了 跟VVDocumenter规范注释生成器的安装方式一样 ...
- Deep Voice
https://arxiv.org/abs/1702.07825 听起来和真人声非常接近了.
- go语言中文网中的资源
https://studygolang.com/subject/2 Go 系列教程 https://studygolang.com/subject/74 Go 语言机制 https://s ...
- Java -- 异常的捕获及处理 -- Exception类与RuntimeException类
7.3 Exception类与RuntimeException类 Exception类与RuntimeException类的联系与区别??? 例:字符串变为整型 Class : RuntimeExce ...
- 树莓派(Raspberry Pi)USB无线网卡自动连接,二代B
Raspberry Pi 使用USB无线网卡的时候不会因为路由重启而掉线. #!/bin/bash while true ; do if ifconfig wlan0 | grep -q " ...
- ios开发之--把秒转换为天时分秒
把秒转换成时分秒: - (NSString *)timeFormatted:(int)totalSeconds { ; ) % ; ; return [NSString stringWithForma ...
- iOS try catch
最近看一些第三方的代码有@try,一副看不懂的样子,真心没用过,于是查了些资料收集在这里,以后遇到就不会再蒙比了.其实这东西确实不怎么用,下文有解释.Objective-C 异常机制 :-- 作用 : ...
- iOS开发-Autolayout小结
昨天重点学习了下Autolayout,把以前没有吃透的概念重新理解了下,现在感觉整个人都是棒棒哒. 上一个项目是使用计算坐标代码的方式来布局UI界面,这种方式的缺点在于当苹果发布新尺寸设备时需要重写代 ...
- java中调用groovy
Groovy在Java中的应用,做几个小例子以备查 package com.boco.efficiency.groovy; import groovy.lang.Binding; import gro ...
- Ansible 安装和管理服务
ansible 使用 yum 模块来安装软件包,使用 service 模块来启动软件: [root@localhost ~]$ ansible 192.168.119.134 -m yum -a &q ...