You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Pytest生成Allure报告失败:报错AttributeError: module 'allure'无severity_level属性

Fixing "AttributeError: module 'allure' has no attribute 'severity_level'" with Allure + Pytest

First, let's break down why you're seeing this error:
This issue almost always stems from incompatible Allure Python package versions or having outdated/deprecated Allure-related packages installed. Even if your test code doesn't explicitly use allure.severity_level, conflicting dependencies can trigger this error under the hood.

Here's a step-by-step fix:

1. Clean up conflicting Allure packages

First, we'll remove any old or conflicting packages that might be causing issues. Run this in your terminal:

pip uninstall -y pytest-allure-adaptor allure-python-commons allure-pytest

The pytest-allure-adaptor package is deprecated, so it's a common culprit for version conflicts.

2. Install the official, up-to-date Allure Pytest plugin

Next, install the latest maintained version of the Allure plugin for pytest:

pip install --upgrade allure-pytest

3. Run your tests with Allure result generation

Your original command pytest test_ABTesting.py doesn't generate the data needed for an Allure report. You need to add the --alluredir flag to specify where to store test results:

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

This will create a allure-results directory with the necessary JSON files for the report.

4. View the Allure report

Once the tests finish, run this command to start a local web server and view the report:

allure serve ./allure-results

A browser window should open automatically with your formatted test report.


Bonus: Improve your test code (best practices)

Your current code uses a global driver variable in conftest.py, which can lead to test isolation issues (tests interfering with each other). Here's a better approach using pytest fixtures properly:

Updated conftest.py:

import pytest
from selenium import webdriver

@pytest.fixture(autouse=True)
def browser():
    EXE_PATH = r'C:\Users\1602746\Softwares\chromedriver.exe'
    chromeOptions = webdriver.ChromeOptions()
    chromeOptions.add_experimental_option('useAutomationExtension', False)
    
    # Initialize driver
    driver = webdriver.Chrome(executable_path=EXE_PATH, options=chromeOptions)
    driver.implicitly_wait(10)
    driver.maximize_window()
    driver.get('http://the-internet.herokuapp.com/')
    
    yield driver  # Pass driver to tests
    
    # Teardown
    driver.quit()

Updated test_ABTesting.py:

import pytest
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait

@pytest.fixture
def go_to_ab_testing_page(browser):
    wait = WebDriverWait(browser, 10)
    wait.until(expected_conditions.element_to_be_clickable(
        (By.XPATH, "//a[starts-with(@href, '/abtest')]")
    )).click()
    return browser

def test_ab_test_header(go_to_ab_testing_page):
    header_text = go_to_ab_testing_page.find_element(By.CSS_SELECTOR, "h3").text
    assert 'A/B Test' in header_text

def test_ab_test_paragraph(go_to_ab_testing_page):
    expected_paragraph = (
        "Also known as split testing. This is a way in which businesses are able to "
        "simultaneously test and learn different versions of a page to see which text "
        "and/or functionality works best towards a desired outcome (e.g. a user action "
        "such as a click-through)."
    )
    paragraph_text = go_to_ab_testing_page.find_element(
        By.XPATH, f"//p[contains(text(),'{expected_paragraph}')]"
    ).text
    assert expected_paragraph in paragraph_text

Key improvements here:

  • No more global variables: The browser fixture is passed directly to tests and other fixtures, ensuring test isolation.
  • Used modern Selenium syntax (find_element(By.CSS_SELECTOR, ...) instead of the deprecated find_element_by_css_selector).
  • Extracted the long expected paragraph to a variable for better readability.

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

火山引擎 最新活动