pytest框架_pytest_runtest_makereport实现失败截图自动加入allure

效果如图:
在这里插入图片描述
代码分析:

# 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

# test_1.py 测试用例层代码

import pytest, os
from selenium import webdriver
import allure


def test_login(browser):
    with allure.step("step1:打开登录首页"):
        browser.get("https://qzone.qq.com/")
    with allure.step("step2:输入账号:admin"):
        browser.find_element_by_name("u").send_keys("admin")
    with allure.step("step2:输入密码:123456"):
        browser.find_element_by_name("p").send_keys("123456")


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

钩子函数—代码分析:

pytest提供的很多钩子(Hooks)方法方便我们对测试用例框架进行二次开发,可以根据自己的需求进行改造。
先学习下pytest_runtest_makereport这个钩子方法,可以更清晰的了解用例的执行过程,并获取到每个用例的执行结果。
先看下相关的源码,在_pytest/runner.py下,可以导入之后,点进去查看
def pytest_runtest_makereport(item, call):
“”" return a :py:class:_pytest.runner.TestReport object
for the given :py:class:pytest.Item and
:py:class:_pytest.runner.CallInfo.
“”"
这里item是测试用例,call是测试步骤,具体执行过程如下:
先执行when=‘setup’ 返回setup 的执行结果
然后执行when=‘call’ 返回call 的执行结果
最后执行when='teardown’返回teardown 的执行结果

(2)装饰器:@pytest.hookimpl() >>> 修改pytest-html报告
Hook函数的定义
Hook函数又称为钩子函数,它的作用可以理解成钩住自己喜欢的东西(在window中,喜欢的东西可理解为消息),然后对自己喜欢的东西单独做处理

# 对于给定的测试用例(item)和调用步骤(call),返回一个测试报告对象(_pytest.runner.TestReport)
@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
    """
  每个测试用例执行后,制作测试报告
  :param item:测试用例对象
  :param call:测试用例的测试步骤
           执行完常规钩子函数返回的report报告有个属性叫report.when
            先执行when=’setup’ 返回setup 的执行结果
            然后执行when=’call’ 返回call 的执行结果
            最后执行when=’teardown’返回teardown 的执行结果
    :return:
    """
    # 获取常规钩子方法的调用结果,返回一个result对象 
    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")

            # 添加1个测试测试步骤为了防截图
            with allure.step('添加失败截图...'):
                # 插入截图的详细信息
                allure.attach(driver.get_screenshot_as_png(), "失败截图", allure.attachment_type.PNG)

http://www.51testing.com/html/42/n-4474642.html
https://blog.csdn.net/u011035397/article/details/109546814

猜你喜欢

转载自blog.csdn.net/weixin_45451320/article/details/113926107
今日推荐