pytest-fixture-config
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
-
ModuleNotFoundError: No module named 'pytest_fixture_config'
cause The `pytest-fixture-config` package is not installed or not accessible in the current Python environment.fixRun `pip install pytest-fixture-config` to install the package. -
pytest.FixtureNotFound: Fixture 'my_config_fixture' not found
cause A test or another fixture is requesting a fixture that either isn't defined, is misspelled, or isn't discoverable by pytest (e.g., in a `conftest.py` that's out of scope).fixCheck the spelling of the fixture name, ensure the fixture is defined within a `conftest.py` in an appropriate directory, or explicitly imported if it's not a root-level plugin fixture. If using `pytest_plugins = ['pytest_fixture_config']`, verify it's correctly specified. -
AttributeError: 'MyAppConfig' object has no attribute 'new_attribute'
cause You are attempting to access an attribute on a `Config` subclass instance that was not declared in its `__slots__` attribute.fixAdd the 'new_attribute' to the `__slots__` tuple in your `MyAppConfig` class definition. Remember that classes using `__slots__` do not support dynamic attribute assignment outside of the defined slots.
Warnings
- breaking Python 2 and Python <3.6 support has been dropped. All compatibility code for older Python versions was removed in v1.8.0.
- breaking The `mock` package is no longer used. All mock-related functionality now uses the standard library's `unittest.mock` module.
- breaking Usage of `path.py` and `path` has been removed in favor of `pathlib` for file system path operations.
- gotcha Ensure your `pytest-fixture-config` plugin is discoverable by pytest. It must be installed in the environment where pytest runs, or explicitly enabled if not using setuptools entry points.
Install
-
pip install pytest-fixture-config
Imports
- Config
from pytest_fixture_config import Config
- requires_config
from pytest_fixture_config import requires_config
- yield_requires_config
from pytest_fixture_config import yield_requires_config
Quickstart
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)