uiautomator2+app ui自动化用例报错截图pytest_runtest_makereport

发布时间 2023-04-23 11:26:32作者: Receiver
  • pytest提供了pytest_runtest_makereport这个方法,可以捕获用例的执行情况。根据官方提供的示例,在conftest.py文件中添加如下代码就可以捕获每个用例的执行结果。
  • 那么pytest_runtest_makereport作用: 对于给定的测试用例(item)和调用步骤(call), 返回一个测试报告对象(_pytest.runner.TestReport);
  • 这个钩子方法会被每个测试用例调用 3 次,
    ①用例的 setup 执行完毕后,调用 1 次,返回 setup 的执行结果;
    ②用例执行完毕之后,调用 1 次,返回测试用例的执行结果;
    ③用例的 teardown 执行完毕后,调用1 次,返回 teardown 的执行结果;

import allure
import pytest
@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
    outcome = yield
    rep = outcome.get_result()
    if rep.when == "call" and rep.failed:
          png_data = self.d.screenshot(format='raw')
        current_time = time.strftime('_%H:%M:%S_', time.localtime(time.time()))
        current_name = filename + current_time + '.png'
        allure.attach(png_data, name=current_name, attachment_type=allure.attachment_type.PNG)
  1.  item 是测试用例对象;
  2.  call 是测试用例的测试步骤
  3. 将截图放入报告中allure.attach
  4. 当用例执行不成功的时候,就会将错误截图放置报告中