Allure无法读取测试生成的JSON文件且附件未附加的解决方法咨询
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:
.jsonfiles for each test case.pngfiles 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

