一、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带错误截图及失败重测机制的更多相关文章

  1. 『德不孤』Pytest框架 — 1、Pytest测试框架介绍

    目录 1.什么是单元测试框架 2.单元测试框架主要做什么 3.单元测试框架和自动化测试框架有什么关系 4.Pytest测试框架说明 5.Pytest框架和Unittest框架区别 (1)Unittes ...

  2. 【pytest系列】- pytest测试框架介绍与运行

    如果想从头学起pytest,可以去看看这个系列的文章! https://www.cnblogs.com/miki-peng/category/1960108.html 前言​ ​ 目前有两种纯测试的测 ...

  3. python pytest测试框架介绍二

    在介绍一中简单介绍了pytest的安装和简单使用,接下来我们就要实际了解pytest了 一.pytest的用例发现规则 pytest可以在不同的函数.包中发现用例,发现的规则如下 文件名以test_开 ...

  4. python pytest测试框架介绍三

    之前介绍了pytest以xUnit形式来写用例,下面来介绍pytest特有的方式来写用例 1.pytest fixture实例1 代码如下 from __future__ import print_f ...

  5. python pytest测试框架介绍一

    一.安装 pytest不是python默认的package,需要自动手工安装. pytest支持python 2.6--3.5之间的版本,同时可以在unix及windows上安装 安装方式: pip ...

  6. python pytest测试框架介绍五---日志实时输出

    同样的,在使用pytest进行自动化测试时,需要将实时日志打印出来,而不是跑完后才在报告中出结果. 不过,好在pytest在3.3版本开始,就支持这一功能了,而不用再像nose一样,再去装第三方插件. ...

  7. pytest测试框架+jenkins结合pytest+jenkins邮件通知配置

    刚刚做完一个项目,由于这是一个方案项目,而不是产品,所以各种准备很不充分,很多公司的能力不能复用,整个团队又都是新员工,而且有部分实习生,匆忙上马,今天对我的自动化框架做一个回溯 自动化测试框架的选择 ...

  8. pytest测试框架 -- 简介

    一.pytest测试框架简介: (1)pytest是python的第三方测试框架,是基于unittest的扩展框架,比unittest更简洁,更高效. (2)pytest框架可以兼容unittest用 ...

  9. Pytest测试框架(五):pytest + allure生成测试报告

    Allure 是一款轻量级.支持多语言的开源自动化测试报告生成框架,由Java语言开发,可以集成到 Jenkins. pytest 测试框架支持Allure 报告生成. pytest也可以生成juni ...

随机推荐

  1. man 转 pdf _____ jpg 转 pdf

    man -t bash | ps2pdf - bash.pdf 将jpeg转成pdf convert *.jpeg images.pdf 将pdf转成jpeg gs -dBATCH -dNOPAUSE ...

  2. p12证书转keystore签名

    java https://my.oschina.net/u/555639/blog/524821   AIR p12转keystore证书签名apk 2014年01月03日 ⁄ 移动探索 ⁄ 评论数 ...

  3. java登录央行征信网站

    package com.entrym.crawler.test; import java.util.HashMap; import java.util.Map; import org.apache.c ...

  4. ajax之cache血与泪~~

    场景:项目以ie5渲染页面,点击导出列表数据(Excel形式),点击导出发送get请求,后台生成Excel文件,返回文件地址信息 异常:ie第一次返回的信息正常,之后返回的都是第一次的结果,googl ...

  5. Java显示指定类型的文件

    文件作为存储数据的单元,会根据数据类型产生很多分类,也就是所谓的文件类型.在对数据文件进行操作时,常常需要根据不同的文件类型来作不同的处理.本实例实现的是读取文件夹指定类型的文件并显示到表格控件中.这 ...

  6. WPF依赖属性相关博客导航

    1.一站式WPF--依赖属性(DependencyProperty)一(什么是依赖属性,依赖属性的由来) 2.一站式WPF--依赖属性(DependencyProperty)二(涉及依赖属性的使用) ...

  7. Unity关闭shader中的光照模型以及如何自定义光照模型

    // Upgrade NOTE: replaced '_World2Object' with 'unity_WorldToObject' // Upgrade NOTE: replaced '_Wor ...

  8. TOMCAT可以稳定支持的最大并发用户数

    转自:http://blog.sina.com.cn/s/blog_68b7d2f50101ann7.html 服务器配置: 单硬盘,SATA   8MB缓存 测试服务器和loadrunner运行服务 ...

  9. 天猫浏览型应用的CDN静态化架构演变(转)

    转自:http://wbj0110.iteye.com/blog/2036613 在天猫双11活动中,商品详情.店铺等浏览型系统,通常会承受超出日常数倍甚至数十倍的流量冲击.随着历年来双11流量的大幅 ...

  10. QT之 Hello World

    下载……   我下载的Qt creater 版本为4.2.1,Qt版本为5.8.0 打开QT Creater 1. 新建项目 New Project -> Application -> Q ...