0
0
Selenium-pythonHow-ToBeginner · 4 min read

How to Use Allure with Selenium Python for Test Reporting

To use Allure with Selenium Python, install the allure-pytest package and add @allure.step decorators in your test code. Run tests with pytest and generate reports using allure serve or allure generate commands.
📐

Syntax

Here is the basic syntax to integrate Allure with Selenium tests in Python using pytest:

  • @allure.step: Decorator to mark a function or method as a step in the report.
  • Use pytest as the test runner with --alluredir=DIR option to save results.
  • Generate or serve the report with allure generate DIR -o REPORT_DIR or allure serve DIR.
python
import pytest
import allure
from selenium import webdriver

@allure.step('Open browser and navigate to URL')
def open_url(driver, url):
    driver.get(url)

@allure.step('Check page title contains expected text')
def check_title(driver, expected):
    assert expected in driver.title

@pytest.fixture
@allure.step('Setup WebDriver')
def driver():
    driver = webdriver.Chrome()
    yield driver
    driver.quit()

@allure.feature('Google Search')
@allure.story('Title verification')
def test_google_title(driver):
    open_url(driver, 'https://www.google.com')
    check_title(driver, 'Google')
💻

Example

This example shows a simple Selenium test with Allure steps and how to run it to generate a report.

Run the test with pytest --alluredir=allure-results to save results, then generate the report with allure serve allure-results.

python
import pytest
import allure
from selenium import webdriver

@allure.step('Open Google homepage')
def open_google(driver):
    driver.get('https://www.google.com')

@allure.step('Verify title contains Google')
def verify_title(driver):
    assert 'Google' in driver.title

@pytest.fixture
def driver():
    driver = webdriver.Chrome()
    yield driver
    driver.quit()

@allure.feature('Google Tests')
@allure.story('Homepage Title')
def test_google_homepage_title(driver):
    open_google(driver)
    verify_title(driver)
Output
============================= test session starts ============================== collected 1 item test_sample.py . [100%] ============================== 1 passed in 5.12s ===============================
⚠️

Common Pitfalls

  • Not installing allure-pytest or missing pytest integration causes no report generation.
  • Forgetting to run pytest with --alluredir option means no results saved.
  • Not having Allure commandline installed or not in PATH causes allure serve to fail.
  • Using @allure.step incorrectly (e.g., on non-callable objects) will not show steps.

Always ensure WebDriver is properly closed to avoid hanging tests.

python
## Wrong way (missing allure step decorator):

def open_url(driver, url):
    driver.get(url)

## Right way:

import allure

@allure.step('Open URL {url}')
def open_url(driver, url):
    driver.get(url)
📊

Quick Reference

Summary of key commands and decorators for Allure with Selenium Python:

Command/DecoratorPurpose
@allure.stepMark a function as a step in the report
@allure.featureGroup tests by feature
@allure.storyGroup tests by user story or scenario
pytest --alluredir=DIRRun tests and save Allure results to DIR
allure serve DIRGenerate and open Allure report from DIR
allure generate DIR -o REPORT_DIRGenerate Allure report files in REPORT_DIR

Key Takeaways

Install allure-pytest and Allure commandline to enable reporting.
Use @allure.step decorators to add meaningful steps in your Selenium tests.
Run pytest with --alluredir option to save test results for Allure.
Generate or serve reports using Allure CLI commands after test execution.
Ensure WebDriver is properly managed to avoid test failures or hangs.