Reusable configuration and artifact utilities for building pytest plugins
Project description
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:
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)
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
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pytest_plugin_utils-0.1.0.tar.gz.
File metadata
- Download URL: pytest_plugin_utils-0.1.0.tar.gz
- Upload date:
- Size: 6.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.10.1 {"installer":{"name":"uv","version":"0.10.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2e12971d46eebefda7c99d3936be946a51482e1519db59855ef1104cefa6c852
|
|
| MD5 |
001d86fac3cf0fbd941a8e873aa780eb
|
|
| BLAKE2b-256 |
de6ae5ee2c91f789c1f71a92c76328d62aee0b885d210396e479e25e102921c5
|
File details
Details for the file pytest_plugin_utils-0.1.0-py3-none-any.whl.
File metadata
- Download URL: pytest_plugin_utils-0.1.0-py3-none-any.whl
- Upload date:
- Size: 7.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.10.1 {"installer":{"name":"uv","version":"0.10.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cf0d359db06cd28abbb9197ea46ca643dc227ff59a89f095081272d2c52d829c
|
|
| MD5 |
0513d85865277361affb4bfedfeb6ba0
|
|
| BLAKE2b-256 |
5f6010b0a7a1b6a705eebdee9948b8a03042c723c05d41ddcd79589695183c6e
|