pytest-fixture-config

1.8.0 · active · verified Thu Apr 16

pytest-fixture-config is a pytest plugin that provides utility functions for configuring Py.test fixtures. It enables the creation of simple configuration objects for fixtures, allowing tests to be skipped dynamically if required configuration variables are not set. The library is part of the larger `man-group/pytest-plugins` monorepo, is currently in version 1.8.0, and maintains an active release cadence with frequent updates.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to define a configuration class using `pytest_fixture_config.Config`, populate it from environment variables, and use the `requires_config` decorator to create fixtures that conditionally skip tests if necessary configuration values are not provided. This ensures tests requiring specific setups only run when their prerequisites are met.

import os
import pytest
import subprocess
from pytest_fixture_config import Config, requires_config

# 1. Define your configuration class inheriting from Config
class MyAppConfig(Config):
    __slots__ = ('api_key', 'base_url') # Define expected config attributes

# 2. Create a singleton instance, reading from environment variables
APP_CONFIG = MyAppConfig(
    api_key=os.environ.get('MY_APP_API_KEY'), # No default, required
    base_url=os.environ.get('MY_APP_BASE_URL', 'https://api.example.com') # With default
)

# 3. Use requires_config decorator to make fixtures dependent on config
@pytest.fixture
@requires_config(APP_CONFIG, ['api_key'])
def authenticated_client():
    """Provides an authenticated client if API_KEY is set."""
    print(f"\nUsing API Key: {APP_CONFIG.api_key[:5]}... and Base URL: {APP_CONFIG.base_url}")
    # In a real app, this would initialize an API client
    class MockClient:
        def fetch_data(self): return {"status": "data_fetched"}
    return MockClient()

def test_data_fetch(authenticated_client):
    """This test requires a valid API_KEY to run."""
    assert authenticated_client.fetch_data() == {"status": "data_fetched"}

# To run this example:
# 1. Save as `test_app.py`
# 2. Run: `pytest -s test_app.py` (it will be skipped)
# 3. Set env var: `export MY_APP_API_KEY="your_secret_key"`
# 4. Run again: `pytest -s test_app.py` (it should pass)

view raw JSON →