@pytest.hookimpl(tryfirst=True, hookwrapper=True) def pytest_runtest_makereport(item, call): # execute all other hooks to obtain the report object outcome = yield rep = outcome.get_result() # rep可以拿到用例的执行结果详情 if rep.when == "call" and rep.failed: im = ImageGrab.grab() # 使用 io.BytesIO 来捕获截图的二进制数据(PNG 格式) binary_image = io.BytesIO() im.save(binary_image, format='PNG') # 将字节流的位置重置到开头,以便我们可以读取数据 binary_image.seek(0) # 现在 binary_image.read() 将返回截图的二进制数据(PNG 格式) png_binary_data = binary_image.read() allure.attach(png_binary_data, "异常截图", allure.attachment_type.PNG)
标签:binary,截图,rep,用例,pytest,image,PNG From: https://www.cnblogs.com/liu-Gray/p/18278835