pytest文档47-allure报告添加用例失败截图

前言

使用 selenium 做 web 自动化的时候,很多小伙伴希望用例失败的时候能截图,把异常截图展示到allure报告里面。
pytest 有个很好的钩子函数 pytest_runtest_makereport 可以获取到用例执行的结果,所以我们在这个钩子函数里面判断用例失败后截图就可以了。
allure报告添加截图可以使用 allure.attach 方法

失败截图

关于 pytest_runtest_makereport  获取用例执行结果的钩子函数介绍可以看前面这篇https://www.cnblogs.com/yoyoketang/p/12609871.html
在 conftest.py 文件写用例执行的钩子函数

import pytest
from selenium import webdriver
import os
import allure
# 作者-上海悠悠 QQ交流群:717225969
# blog地址 https://www.cnblogs.com/yoyoketang/

_driver = None

@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
'''
获取每个用例状态的钩子函数
:param item:
:param call:
:return:
'''
# 获取钩子方法的调用结果
outcome = yield
rep = outcome.get_result()
# 仅仅获取用例call 执行结果是失败的情况, 不包含 setup/teardown
if rep.when == "call" and rep.failed:
mode = "a" if os.path.exists("failures") else "w"
with open("failures", mode) as f:
# let's also access a fixture for the fun of it
if "tmpdir" in item.fixturenames:
extra = " (%s)" % item.funcargs["tmpdir"]
else:
extra = ""
f.write(rep.nodeid + extra + "\n")
# 添加allure报告截图
with allure.step('添加失败截图...'):
allure.attach(_driver.get_screenshot_as_png(), "失败截图", allure.attachment_type.PNG)

@pytest.fixture(scope='session')
def browser():
global _driver
if _driver is None:
_driver =webdriver.Chrome()
yield _driver
print("1111111111")
_driver.quit()

用例运行

import pytest
from selenium import webdriver
import allure
# 作者-上海悠悠 QQ交流群:717225969
# blog地址 https://www.cnblogs.com/yoyoketang/

def test_login(browser):
with allure.step("step1:打开登录首页"):
browser.get("http://ip:6009/admin/login/?next=/admin/")
with allure.step("step2:输入账号:admin"):
browser.find_element_by_name("username").send_keys("admin")
with allure.step("step2:输入密码:123456"):
browser.find_element_by_name("password").send_keys("123456")
# 故意断言失败,看是否会截图
assert browser.title == "悠悠"

整个目录结构如下

运行用例后,截图会存到./report 报告目录,allure报告展示

《当当网》开学季促销活动力度,每满100减50

券使用规则(挑选当当网自营店书籍)

200-40 优惠券码:KRQ9PY   500张

使用范围:8月21号-9月7号

200-40 叠加券  全场通用

使用教程可以参考前面的这篇《当当网》全场半价基础上,再送200-30满减券

(0)

相关推荐

  • Python Requests Pytest YAML Allure实现接口自动化

    作者:wintest 链接:https://www.cnblogs.com/wintest/p/13423231.html 本项目实现接口自动化的技术选型:Python+Requests+Pytest ...

  • pytest参数化-读取excel allure报告展示

    由于近期公司要求项目接口自动化且使用参数化.装饰器等,我在网上查了一下资料,现在整理下,放便以后代码套用 版本: pytest==6.2.1 pytest-html ==2.1.1 pyyaml == ...

  • appium

    文章目录 appium 1.appium可以测试的对象 2.自动化混合APP的条件 3.总结 4.查看webview版本 5.APP面试点 6.代码部分 方式1: 设置搜索webview 方式2: 输 ...

  • pytest文档41-参数化 ids 用例描述为中文时控制台输出unicode

    前言 使用 pytest.mark.parametrize  参数化的时候,加 ids 参数用例描述有中文时,在控制台输出会显示unicode编码,中文不能正常显示. 使用 pytest_collec ...

  • pytest文档44-allure.dynamic动态生成用例标题

    前言 pytest 结合 allure 描述用例的时候我们一般使用 @allure.title 和 @allure.description 描述测试用例的标题和详情. 在用例里面也可以动态更新标题和详 ...

  • pytest文档39-参数化(parametrize)结合allure.title()生成不同标题报告

    前言 pytest的参数化(parametrize)可以实现只需维护测试数据,就能生成不同的测试用例目的.可以在参数化的时候加 ids 参数对每个用例说明使用场景. 最终我们希望在 allure 报告 ...

  • pytest文档7-生成html报告

    前言 pytest-HTML是一个插件,pytest用于生成测试结果的HTML报告.兼容Python 2.7,3.6 pytest-html 1.github上源码地址[https://github. ...

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

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

  • pytest文档13-allure2生成html报告(史上最详细)

    前言 allure是一个report框架,支持java的Junit/testng等框架,当然也可以支持python的pytest框架,也可以集成到Jenkins上展示高大上的报告界面. 环境准备 1. ...

  • pytest文档20-pytest-html报告优化

    前言 pytest-html测试报告默认是不展示用例描述Description内容,之前用unittest生成的报告是可以展示用例的描述,也就是test开头的用例下三个引号里面的注释(docstrin ...

  • pytest文档21-pytest-html报告优化

    前言 pytest-html报告中当用到参数化时候,获取用例的nodeid里面有中文时候,会显示[\u6350\u52a9\u6211\u4eec]这种编码(再次声明,这个不叫乱码,这是unicode ...

  • pytest文档45-allure添加环境配置(environment)

    前言 在 allure 报告首页 ENVIRONMENT 显示 'There are no environment variables' 没有环境变量的配置信息. 环境变量配置可以添加报告相关的配置参 ...