实用指南:使用Pytest Allure测试框架添加用例失败截图

简介: 本文介绍了如何在使用`allure+pytest`进行软件测试时,通过`pytest_runtest_makereport`钩子函数自动捕获失败用例的截图。在`conftest.py`中定义钩子,当用例失败时,保存截图并附加到Allure测试报告中。测试代码示例展示了登录豆瓣的场景,测试失败时会自动生成截图。这种方法有助于快速理解和解决测试问题,提升测试效率和软件质量。

image.png

前言

在我们进行软件测试的过程中,我们提交的测试报告缺少一些详细的附件,尤其是用例失败时候的截图,更方便我们去查看具体的情况,我们在进行测试时会使用allure+pytest来生成测试报告,本文我们就来介绍一下在allure测试报告中添加用例失败截图。

钩子函数准备

我们可以使用pytest_runtest_makereport来获取获取用例执行结果,因此我们可以先在conftest.py写一下我们的钩子函数,如下:

# coding=utf-8
import pytest, os, allure
from selenium import webdriver
import xlrd


# 用例失败后自动截图
@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
    """
    获取用例执行结果的钩子函数
    :param item:
    :param call:
    :return:
    """
    outcome = yield
    report = outcome.get_result()
    if report.when == "call" and report.failed:
        mode = "a" if os.path.exists("failures") else "w"
        with open("failures", mode) as f:
            if "tmpir" in item.fixturenames:
                extra = " (%s)" % item.funcargs["tmpdir"]
            else:
                extra = ""
                f.write(report.nodeid + extra + "\n")
            with allure.step('添加失败截图...'):
                allure.attach(driver.get_screenshot_as_png(), "失败截图", allure.attachment_type.PNG)


@pytest.fixture(scope='session', autouse=True)
def browser():

    global driver
    driver = webdriver.Chrome()
    return driver

测试用例

我们以登录豆瓣为例,写一下我们的测试代码,如下:

import pytest, os
from selenium import webdriver
import allure


def test_login(browser):
    with allure.step("step1:打开登录首页"):
        browser.get("https://accounts.douban.com/passport/login")
    with allure.step("step2:输入账号"):
        browser.find_element_by_name("username").send_keys("xxxxxxx")
    with allure.step("step2:输入密码"):
        browser.find_element_by_name("password").send_keys("xxxxxx")


if __name__ == "__main__":
    pytest.main(['--alluredir', '../report', 'test_2.py::test_login'])
    # allure转换成---html并打开测试报告
    os.system('cd D:/se_frame/Reports/allure_data')
    os.system('allure generate ../report -o report/html --clean')

总结

通过结合Pytest和Allure框架,我们可以轻松地为测试用例添加失败时的截图功能,从而更好地理解测试结果并加快故障排查的速度。希望本文能够帮助大家更好地利用Pytest和Allure框架进行测试,并提高软件质量。

相关文章
|
3月前
|
测试技术 Shell API
Playwright系列(3):运行测试用例
Playwright系列(3):运行测试用例
|
3月前
|
测试技术 Python
cypress 和allure 集成生成测试报告
cypress 和allure 集成生成测试报告
cypress 和allure 集成生成测试报告
|
9月前
|
测试技术
26-pytest-allure描述用例详解
26-pytest-allure描述用例详解
|
9月前
|
测试技术 C++ Python
【pytest】pytest的几种运行方式,尤其最后一种调试很方便
【pytest】pytest的几种运行方式,尤其最后一种调试很方便
|
9月前
|
前端开发 测试技术
09-Httprunner-生成测试报告
09-Httprunner-生成测试报告
09-Httprunner-生成测试报告
|
自然语言处理 Java 测试技术
pytest学习和使用21-测试报告插件allure-pytest如何使用?
pytest学习和使用21-测试报告插件allure-pytest如何使用?
120 0
pytest学习和使用21-测试报告插件allure-pytest如何使用?
|
测试技术 Python
pytest学习和使用15-Pytest用例失败如何重跑?(pytest-rerunfailures的简单使用)
pytest学习和使用15-Pytest用例失败如何重跑?(pytest-rerunfailures的简单使用)
70 0
|
测试技术
pytest学习和使用14-Pytest用例执行结果有哪几种状态?
pytest学习和使用14-Pytest用例执行结果有哪几种状态?
76 0
|
测试技术 Python
pytest学习和使用2-初步使用和用例运行
pytest学习和使用2-初步使用和用例运行
68 0
pytest学习和使用2-初步使用和用例运行
|
测试技术
pytest二位元组方式获取测试用例
二次封装测试用例读取方式
84 0

热门文章

最新文章