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
pytestas the test runner with--alluredir=DIRoption to save results. - Generate or serve the report with
allure generate DIR -o REPORT_DIRorallure 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-pytestor missingpytestintegration causes no report generation. - Forgetting to run pytest with
--allurediroption means no results saved. - Not having Allure commandline installed or not in PATH causes
allure serveto fail. - Using
@allure.stepincorrectly (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/Decorator | Purpose |
|---|---|
| @allure.step | Mark a function as a step in the report |
| @allure.feature | Group tests by feature |
| @allure.story | Group tests by user story or scenario |
| pytest --alluredir=DIR | Run tests and save Allure results to DIR |
| allure serve DIR | Generate and open Allure report from DIR |
| allure generate DIR -o REPORT_DIR | Generate 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.