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

参考以上链接官网解释

这里我给出我的结构供参考:

  1. project/
  2. |- __init__.py
  3. |- conftest.py
  4. |- bar/
  5. |- __init__.py
  6. |- tests/
  7. |- __init__.py
  8. |- test_foo.py

conftest.py

  1. # coding=utf-8
  2. from selenium import webdriver
  3. import pytest
  4. import os
  5. driver = None
  6. @pytest.mark.hookwrapper
  7. def pytest_runtest_makereport(item):
  8. """
  9. Extends the PyTest Plugin to take and embed screenshot in html report, whenever test fails.
  10. :param item:
  11. """
  12. pytest_html = item.config.pluginmanager.getplugin('html')
  13. outcome = yield
  14. report = outcome.get_result()
  15. extra = getattr(report, 'extra', [])
  16. if report.when == 'call' or report.when == "setup":
  17. xfail = hasattr(report, 'wasxfail')
  18. if (report.skipped and xfail) or (report.failed and not xfail):
  19. dirpng=r'./report/png/'
  20. if os.path.exists(dirpng) and os.path.isdir(dirpng):
  21. pass
  22. else:
  23. os.mkdir(dirpng)
  24. file_name = dirpng + report.nodeid.replace("::", "_")+".png"
  25. file_name1=r'./png/'+ report.nodeid.replace("::", "_")+".png"
  26. _capture_screenshot(file_name)
  27. if file_name:
  28. html = '<div><img src="%s" alt="screenshot" style="width:304px;height:228px;" ' \
  29. 'onclick="window.open(this.src)" align="right"/></div>' % file_name1
  30. extra.append(pytest_html.extras.html(html))
  31. report.extra = extra
  32. def _capture_screenshot(name):
  33. driver.get_screenshot_as_file(name)
  34. @pytest.fixture(scope='session', autouse=True)
  35. def browser():
  36. global driver
  37. if driver is None:
  38. driver = webdriver.Chrome()
  39. return driver

3例脚本编写

  1. # coding=utf-8
  2. import pytest
  3. class TestFailScreenShoot(object):
  4. def test_run001(self):
  5. assert 1==2
  6. def test_run002(self):
  7. assert 1==1
  8. def test_screenshot_on_test_failure(self,browser):
  9. browser.get("https://www.baidu.com")
  10. assert False
  11. # if __name__ == "__main__":
  12. # 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 失败重跑截图的更多相关文章

  1. pytest失败重跑

    一.说明 平常在做功能测试的时候,经常会遇到某个模块不稳定,偶然会出现一些bug,对于这种问题我们会针对此用例反复执行多次,最终复现出问题来.自动化运行用例时候,也会出现偶然的bug,可以针对单个用例 ...

  2. pytest文档8-html报告报错截图+失败重跑

    前言 做web自动化的小伙伴应该都希望在html报告中展示失败后的截图,提升报告的档次,pytest-html也可以生成带截图的报告. conftest.py 1.失败截图可以写到conftest.p ...

  3. testng优化:失败重跑,extentReport+appium用例失败截图,测试报告发邮件

    生成的单html方便jenkins集成发邮件,= = 构建失败发邮件 参考:https://blog.csdn.net/galen2016/article/details/77975965 步骤: 1 ...

  4. python3 unittest框架失败重跑加截图支持python2,python3

    github源码地址下载:https://github.com/GoverSky/HTMLTestRunner_cn.git 解压文件后取出/HTMLTestRunner_cn.py文件丢进C:\Py ...

  5. testng增加失败重跑机制

    注: 以下内容引自 http://www.yeetrack.com/?p=1015 testng增加失败重跑机制 Posted on 2014 年 10 月 31 日 使用Testng框架搭建自动测试 ...

  6. 【转载】扩展Robot Framework,实现失败用例自动再执行(失败重跑)

    使用自动化脚本进行测试,经常受环境影响等各方面导致本能成功的脚本失败,下面介绍了RFS框架下,失败重跑的方法: 通过改写RobotFramework源代码增加--retry选项,实现test级别的失败 ...

  7. RF实现多次失败重跑结果合并的基础方法和优化方法

    实现思路:通过分次执行失败案例重跑,然后通过结果文件合并命令实现多次失败重跑结果文件的合并,并输出合并后的log和report文件: 说明:具体失败案例重跑命令和结果文件合并命令请参考本博客其他相关章 ...

  8. testng失败重跑

    重跑失败场景 1.要添加两个文件 背景:因为这里只是想单独展示失败的重跑的案例,所以先暂时把app这块的运行注释掉,只跑一个简单的demo,就一个简单类,类中就3个测试方法,失败重跑的原理是,运行方法 ...

  9. TestNg失败重跑—解决使用 dataProvider 参数化用例次数冲突问题

    问题背景 在使用 testng 执行 UI 自动化用例时,由于 UI自动化的不稳定性,我们在测试的时候,往往会加上失败重跑机制.在不使用 @DataProvider 提供用例参数化时,是不会有什么问题 ...

随机推荐

  1. Spring Boot—06集成前端模板thymeleaf

    Spring Boot建议使用这些模板引擎,避免使用JSP,若一定要使用JSP将无法实现Spring Boot的多种特性 pom.xml <dependency> <groupId& ...

  2. Eclipse 校验取消

    eclipse Multiple annotations found at this line错误,eclipse开发过程中,一些XML配置文件会报错,但是这些其实不是错,飘红的原因是因为eclips ...

  3. java 内存分析之构造方法执行过程

    package Demo; public class BirthDate { private int day; private int month; private int year; public ...

  4. 【转】Twitter Storm: 在生产集群上运行topology

    Twitter Storm: 在生产集群上运行topology 发表于 2011 年 10 月 07 日 由 xumingming 作者: xumingming | 可以转载, 但必须以超链接形式标明 ...

  5. Claims-based认证解析

    Claims-based认证相关的两个重要的类ClaimsIdentity以及ClaimsPrincipal解析 ClaimsIdentity以及ClaimsPrincipal是.NET下Claims ...

  6. Ubuntu16安装GPU版本TensorFlow(个人笔记本电脑)

    想着开始学习tf了怎么能不用GPU,网上查了一下发现GeForce GTX确实支持GPU运算,所以就尝试部署了一下,在这里记录一下,避免大家少走弯路. 使用个人笔记本电脑thinkpadE570,内存 ...

  7. leetCode题解之Reshape the Matrix

    1.题目描述 2.分析 使用了一个队列. 3.代码 vector<vector<int>> matrixReshape(vector<vector<int>& ...

  8. SQLServer Temp tables 数据疑问

    1. 现象 使用Cacti监控,有关于临时表的一个图形 可以看到正在使用的临时表Active Temp Tables的数量非常大,并且在非工作时间,也维持在400个左右.感觉非常奇怪,所以追查下! 2 ...

  9. vuejs electron webpack集成使用

    传统的vue SPA页面在浏览器环境中使用,但是有的时候我们还希望能够做成一个类似于桌面的app在PC上使用,希望不仅可以使用所有的浏览器SPA的功能,你也可能外加host os的功能,比如文件的本地 ...

  10. MyEclipse中修改servlet模板

    1.在MyEclipse目录下搜索com.genuitec.eclipse.wizards,得到搜索结果 com.genuitec.eclipse.wizards_8.4.100.me20091213 ...