首页 > 解决方案 > allure.attach.file 失败并出现错误 AttachmentType 类型的对象不是 JSON 可序列化的

问题描述

我正在尝试将屏幕截图附加到 Allure 报告。但是它一直失败并出现错误:

TyperError: Object of type AttachmentType is not JSON serializable

这是我的 conftest.py 中的代码:

import pytest
import allure

@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
    outcome = yield
    report = outcome.get_result()
    if report.whem == "call" and report.failed:
        with allure.step("Failure screenshot"):
        allure.attach.file(r'D:\image_01.png', allure.attachment_type.PNG)

环境:Windows 10 python 3.8 pytest 6.2.2 allure-pytest 2.8.36 pywinauto 0.6.8

标签: pythonpytestallure

解决方案


位置参数形式导致错误。代码应该是:

allure.attach.file(r'D:\image_01.png', attachment_type=allure.attachment_type.PNG)

推荐阅读