You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Allure无法读取测试生成的JSON文件且附件未附加的解决方法咨询

Fixing Allure JSON Read Failure & Missing Attachments in Your Selenium Test

Hey, I’ve run into similar Allure reporting issues before, and looking at your test code, here are the most likely fixes you can try step by step:

1. Ensure You’re Generating Results to the Correct Directory

First off, if you don’t specify the Allure results directory when running your tests, the generated JSON files might end up in a location allure generate can’t find. Make sure you’re running your tests with the --alluredir flag to explicitly set the results folder:

pytest your_test_file.py --alluredir=./allure-results

Then generate the report pointing directly to that results directory:

allure generate ./allure-results -o ./allure-report

Check if the ./allure-results folder has JSON files and any attached screenshots after running the test—if it’s empty, the problem is definitely in how you’re triggering the test.

2. Fix the Attachment Logic in Your Exception Handler

Looking at your try-except block, there’s a small tweak you can make to ensure the screenshot attaches correctly. Sometimes directly passing browse.get_screenshot_as_png() to allure.attach can cause issues if the driver state is unstable right after a timeout. Try storing the screenshot in a variable first:

except TimeoutException:
    # Capture screenshot first to avoid driver state issues
    error_screenshot = browse.get_screenshot_as_png()
    # Add a clear name to the attachment for better visibility
    allure.attach(
        error_screenshot,
        name="Timeout During Product Removal",
        attachment_type=allure.attachment_type.PNG
    )
    raise AssertionError('Removing failed')

Also, make sure the driver instance (browse) is still valid when you capture the screenshot—if the timeout closed the session, you might need to handle that edge case, but this is less likely if your fixture manages the driver properly.

3. Resolve Dependency Version Conflicts

Outdated or mismatched Allure Python packages are a common culprit for broken JSON reports. Uninstall any old Allure adaptors and install the official, maintained allure-pytest package:

# Uninstall conflicting packages
pip uninstall pytest-allure-adaptor allure-python-commons -y
# Install the latest stable allure-pytest
pip install allure-pytest

This ensures your test framework and Allure are using compatible APIs to generate valid JSON files.

4. Validate the Allure Results Files

After running your test, peek into the allure-results folder. You should see:

  • .json files for each test case
  • .png files for your attachments (if the exception was triggered)

If the JSON files are missing, double-check your pytest command and fixture setup—make sure the test is actually finishing and writing results. If the JSON exists but allure generate still fails, try deleting the allure-results and allure-report folders and re-running everything from scratch to clear cached files.

Give these steps a try—most of the time, it’s either a missing --alluredir flag or a dependency issue that’s causing Allure to fail reading the JSON or missing attachments.

内容的提问来源于stack exchange,提问作者Keith

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 18:07:44