Skip to main content

Enhanced logging integration with Allure reports for pytest

Project description

Pytest Allure Step Logger

PyPI version License: MIT Python

A robust, plug-and-play logging integration for pytest and Allure. Automatically captures, buffers, and attaches logs to Allure test steps—no code changes required. Highly configurable, thread-safe, and designed for clean, isolated test reporting.


Table of Contents


Features

  • Automatic Log Capture: All logging calls are captured and buffered, even outside decorators.
  • Allure Step Integration: Logs are attached to Allure test steps for traceability.
  • Thread-Safe: Each test/thread gets its own buffer.
  • Automatic Cleanup: Log buffer is cleared before each test to prevent log mixing.
  • Highly Configurable: Buffer size, log format, min log level, auto-flush, and more.
  • Zero Code Changes: Works with standard logging calls out of the box.
  • Manual & Decorator Support: Use decorators for step-level logs, or log directly.

Installation

pip install pytest-allure-step

Note: Allure reports require the Allure CLI to be installed and available in your PATH.


Quick Start

Basic Usage with Decorator

import logging
from pytest_allure_step import allure_step

@allure_step("My Test Step")
def my_step():
    logging.info("This log will be captured and attached to the Allure step")
    logging.error("Error messages are also captured")
    return True

def test_example():
    result = my_step()
    assert result

Direct Logging (No Decorator)

from pytest_allure_step import info, warning, error, debug, critical, log

def test_direct_logging():
    info("This is an info message")
    warning("This is a warning")
    error("This is an error")
    debug("Debug message")
    critical("Critical error!")
    log(25, "Custom log level message")
    assert True

Usage

Logging Inside and Outside Decorators

  • Inside @allure_step: Logs are attached to the step and flushed at the end.
  • Outside decorator: Logs are buffered and auto-flushed on buffer overflow. Remaining logs are not attached unless you flush or use a decorator.

Example: Buffer Overflow (Auto-Flush)

from pytest_allure_step import configure, info

def test_buffer():
    configure(buffer_size=3)
    for i in range(5):
        info(f"Message {i}")  # After 3, auto-flush triggers and attaches logs

Manual Log Clearing

from pytest_allure_step import clear_logs, info

def test_manual_clear():
    info("Before clear")
    clear_logs()  # Flushes and clears buffer
    info("After clear")

Configuration

from pytest_allure_step import configure, set_log_level

configure(
    buffer_size=500,
    min_log_level="WARNING",
    log_format="[{timestamp}] {level} | {message}",
    auto_flush=True
)
set_log_level("ERROR")

Configuration Options

Option Default Description
buffer_size 1000 Max logs before auto-flush
include_timestamp True Include timestamps in log messages
log_format "[{timestamp}] {level}: {message}" Custom log format string
auto_flush True Auto-flush on buffer overflow
min_log_level "DEBUG" Minimum log level to capture

Best Practices

  • Use @allure_step for step-level log grouping.
  • For logs outside decorators, call clear_logs() at the end if you want all logs attached.
  • Adjust buffer_size for your test suite’s needs.
  • Use set_log_level to reduce noise in large test runs.

Troubleshooting

  • Logs missing in Allure?
    • Ensure you use the decorator or call clear_logs() at the end of your test.
    • Check your buffer size and auto-flush settings.
  • Allure CLI not found?
    • Install Allure CLI and add it to your PATH.
  • Log mixing between tests?
    • Automatic cleanup is enabled by default; if you see mixing, check for custom threading or multiprocessing.

API Reference

Logging Functions

  • critical(message, *args, **kwargs)
  • error(message, *args, **kwargs)
  • warning(message, *args, **kwargs)
  • info(message, *args, **kwargs)
  • debug(message, *args, **kwargs)
  • log(level, message, *args, **kwargs)

Configuration Functions

  • configure(**kwargs)
  • get_config()
  • reset_config()
  • set_buffer_size(size)
  • set_log_level(level)
  • enable_auto_flush()
  • disable_auto_flush()
  • clear_logs()

Decorators

  • @allure_step(step_name)

Fixtures

  • Automatic log cleaning: A pytest fixture is included and enabled by default, clearing the log buffer before each test.

Behavioral Details

  • Buffering: Logs are buffered in thread-local storage.
  • Auto-Flush: When buffer exceeds buffer_size, logs are attached to Allure and buffer is cleared.
  • Decorator: @allure_step flushes logs at the end of the step.
  • Manual Clear: clear_logs() flushes and clears the buffer.
  • Automatic Cleanup: Buffer is cleared before each test (via fixture).
  • Thread Safety: Each thread/test gets its own buffer.

Requirements

  • Python 3.7+
  • pytest >= 6.0.0
  • allure-pytest >= 2.9.0

Development

Install for Development

git clone https://github.com/deekshith-poojary98/pytest-allure-step.git
cd pytest-allure-step
pip install -e .[dev]

Run Tests

pytest

Code Quality

black pytest_allure_step/
flake8 pytest_allure_step/
mypy pytest_allure_step/

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes (add tests!)
  4. Run the test suite
  5. Submit a pull request

Please see CONTRIBUTING.md and CODE_OF_CONDUCT.md for details.


License

MIT License. See LICENSE.


Changelog

See CHANGELOG.md for release history.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest_allure_step-0.1.0.tar.gz (9.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytest_allure_step-0.1.0-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

Details for the file pytest_allure_step-0.1.0.tar.gz.

File metadata

  • Download URL: pytest_allure_step-0.1.0.tar.gz
  • Upload date:
  • Size: 9.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for pytest_allure_step-0.1.0.tar.gz
Algorithm Hash digest
SHA256 b2a9472172a6c8b077fba757e047ccb08040253bd069d34a464ed71965bbc6c0
MD5 3db35fbcf851759bb199205a48445c07
BLAKE2b-256 0fe8c6afc5f58188000993e7c2772019e4736fccb64348564ad62202e277da93

See more details on using hashes here.

File details

Details for the file pytest_allure_step-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pytest_allure_step-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bcad7cbbce36c401ca5dcb35b0f12ce9863479085450514adb5d32c48cf948c7
MD5 3ce48ecf19554873bc5ba271ab3368e7
BLAKE2b-256 96ac36597953fbcdb6f445b2278b85c4a778dae4c4228a7857481ba86399f318

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page