Why Reports?
The console output is enough for you, but a team needs a clear report: what ran, what failed, why and a screenshot of the failure.
1. pytest-html
bash
pip install pytest-html
pytest --html=reports/report.html --self-contained-html
Open reports/report.html in a browser. It shows a summary, durations and details of each failure.
Add Screenshots to the pytest-html Report
python
# conftest.py
import pytest, base64
@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport(item, call):
outcome = yield
report = outcome.get_result()
if report.when == "call":
driver = item.funcargs.get("driver")
if driver and report.failed:
png = driver.get_screenshot_as_base64()
extra = getattr(report, "extras", [])
from pytest_html import extras
extra.append(extras.png(png))
report.extras = extra
2. Allure Report
Allure makes beautiful interactive reports with steps, history and attachments.
bash
pip install allure-pytest
pytest --alluredir=allure-results
# install the allure command line tool (macOS)
brew install allure
# Windows (Scoop): scoop install allure
# then
allure serve allure-results
python
import allure
@allure.feature("Login")
@allure.story("Valid credentials")
@allure.severity(allure.severity_level.CRITICAL)
def test_login(driver):
with allure.step("Open the login page"):
driver.get("https://the-internet.herokuapp.com/login")
with allure.step("Enter credentials and submit"):
driver.find_element(By.ID, "username").send_keys("tomsmith")
driver.find_element(By.ID, "password").send_keys("SuperSecretPassword!")
driver.find_element(By.CSS_SELECTOR, "button[type='submit']").click()
with allure.step("Check success message"):
assert "secure area" in driver.find_element(By.ID, "flash").text
Attach Screenshots to Allure on Failure
python
# conftest.py
import allure
@pytest.fixture
def driver(request):
drv = webdriver.Chrome()
yield drv
rep = getattr(request.node, "rep_call", None)
if rep and rep.failed:
allure.attach(drv.get_screenshot_as_png(),
name="failure screenshot",
attachment_type=allure.attachment_type.PNG)
allure.attach(drv.page_source, name="page source",
attachment_type=allure.attachment_type.HTML)
drv.quit()
3. JUnit XML for CI Tools
bash
pytest --junitxml=reports/junit.xml
Jenkins, GitHub Actions and Azure DevOps read this format and show test results natively.
4. Logging
python
import logging
logging.basicConfig(
filename="logs/test.log",
level=logging.INFO,
format="%(asctime)s %(levelname)s %(name)s: %(message)s")
log = logging.getLogger("tests")
def test_login(driver):
log.info("Opening login page")
driver.get("https://the-internet.herokuapp.com/login")
log.info("Title: %s", driver.title)
ini
# pytest.ini - live log output and a log file
[pytest]
log_cli = true
log_cli_level = INFO
log_file = logs/pytest.log
log_file_level = DEBUG
| Report | Best for |
| pytest-html | Simple single-file report to email |
| Allure | Rich, interactive team reports and trends |
| JUnit XML | CI dashboards |
| Logs | Debugging what the test did |
A good failure report: Test name, the assertion message, a screenshot, the URL and the log lines. With these, most failures can be understood without rerunning.
Conclusion
Use pytest-html for quick reports, Allure for professional ones, JUnit XML for CI and Python logging for details. Always attach a screenshot when a test fails.
Quick Summary: pytest --html / --alluredir / --junitxml; screenshots on failure; log what you do.