Skip to main content

Reusable configuration and artifact utilities for building pytest plugins

Project description

Release Notes Downloads GitHub CI Status License: MIT

Reusable pytest Plugin Utilities

Building pytest plugins means dealing with the same problems repeatedly: managing configuration options with proper precedence (CLI vs INI vs defaults), creating per-test artifact directories, and sanitizing test names for filesystem paths. This package extracts those common patterns into reusable utilities.

I created this after extracting the config and path handling logic from pytest-playwright-artifacts. Rather than reinvent option handling in every plugin, you can use these utilities to get consistent behavior across pytest plugins.

Installation

uv add pytest-plugin-utils

Usage

Configuration Options

Register pytest options with automatic precedence handling (runtime > CLI > INI > defaults) and type inference.

For Plugin Authors

from pytest_plugin_utils import set_pytest_option, register_pytest_options, get_pytest_option

def pytest_addoption(parser):
    # Define your options (use __package__ for namespace)
    set_pytest_option(
        __package__,
        "api_url",
        default="http://localhost:3000",
        help="API base URL",
        available="all",  # Expose via CLI and INI
        type_hint=str,
    )

    # Register them with pytest
    register_pytest_options(__package__, parser)

def pytest_configure(config):
    # Retrieve with automatic type casting
    api_url = get_pytest_option(__package__, config, "api_url", type_hint=str)

The available Parameter

The available argument in set_pytest_option controls how the option is exposed to the user:

Value Exposure Description
"all" CLI & INI Adds a --flag and an INI entry.
"cli_option" CLI Only Adds a --flag but no INI entry.
"ini" INI Only Adds an INI entry but no CLI flag.
None Runtime Only Purely programmatic. No CLI or INI exposure.

Advanced: Handling Complex Types (Dictionaries)

For rich data structures like dictionaries that are difficult to express as strings on the command line, use available=None. This allows you to pass Python objects directly via hooks.

Plugin Author:

set_pytest_option(
    __package__,
    "playwright_kwargs",
    default={},
    available=None,  # Runtime only
    type_hint=dict,
)

Plugin User (in conftest.py):

def pytest_configure(config):
    # Set the dictionary directly on the config object
    config.option.playwright_kwargs = {
        "timeout": 5000,
        "full_page": True,
        "clip": {"x": 0, "y": 0, "width": 800, "height": 600}
    }

For Plugin Users

Once a plugin has registered options using this package, users can configure them in three ways (in order of precedence):

  1. Command Line (highest priority):

    pytest --api-url=https://prod.example.com
    
  2. INI Configuration (medium priority):

    In pytest.ini:

    [pytest]
    api_url = https://staging.example.com
    

    Or in pyproject.toml:

    [tool.pytest.ini_options]
    api_url = "https://staging.example.com"
    
  3. Runtime/Programmatic (via conftest.py):

    def pytest_configure(config):
        # Override at runtime
        config.option.api_url = "https://custom.example.com"
    

The value resolution follows this precedence chain, with each level overriding the next: Runtime > CLI > INI > Default.

Artifact Directory Management

Create per-test artifact directories with sanitized names:

from pytest_plugin_utils import set_artifact_dir_option, get_artifact_dir

def pytest_configure(config):
    # Configure which option name to use (use __package__ for namespace)
    set_artifact_dir_option(__package__, "my_plugin_output")

def pytest_runtest_setup(item):
    # Get a clean directory for this specific test
    artifact_dir = get_artifact_dir(__package__, item)
    # Returns: /output/test-file-py-test-name-param/

Features

  • Centralized option registry with runtime, CLI, and INI support
  • Automatic INI type inference from Python type hints (bool, str, list[str], list[Path])
  • Smart value casting with fallback precedence handling
  • Filesystem-safe test name sanitization for artifact paths
  • Per-test artifact directory creation and resolution
  • Type-safe configuration retrieval with warnings on mismatches

Related Projects

MIT License


This project was created from iloveitaly/python-package-template

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_plugin_utils-0.3.0.tar.gz (7.8 kB view details)

Uploaded Source

Built Distribution

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

pytest_plugin_utils-0.3.0-py3-none-any.whl (8.9 kB view details)

Uploaded Python 3

File details

Details for the file pytest_plugin_utils-0.3.0.tar.gz.

File metadata

  • Download URL: pytest_plugin_utils-0.3.0.tar.gz
  • Upload date:
  • Size: 7.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.1 {"installer":{"name":"uv","version":"0.11.1","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for pytest_plugin_utils-0.3.0.tar.gz
Algorithm Hash digest
SHA256 1676ed31ea9e7fa30b7ee9664d02fec4ddf96df691c1279f24ed9042a48af48e
MD5 29f13671a1b324df2f12089ce11cbc40
BLAKE2b-256 2a357c400aae4d43b6b3829ff6debd876f7c8275ddf8b53a303067d825f34199

See more details on using hashes here.

File details

Details for the file pytest_plugin_utils-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: pytest_plugin_utils-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 8.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.1 {"installer":{"name":"uv","version":"0.11.1","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for pytest_plugin_utils-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 08946450df8ecdea5d778c1208a2e55c558984e295fe79fe41606cbc68b46529
MD5 fade7625516689578015e48b4407a9ad
BLAKE2b-256 25471906bc96d9803589b7e75c8660c38fb228924e4e569d1acb4fc5c69c5242

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