Registry / testing / pytest-variables

pytest-variables

JSON →
library3.1.0pypypiunverified

pytest-variables is a plugin for pytest that provides a mechanism to supply external variables to tests and fixtures. It allows defining variables in various file formats (JSON, YAML, TOML, HJSON) and making them accessible as a dictionary fixture within your pytest test suite. The current version is 3.1.0, and it follows a regular release cadence with major versions indicating breaking changes.

pip install pytest-variables
INSTALL
IMPORT
SIG · PYTEST-VARIABLES
P
pytest-variables
testingpythonv3.1.0
Install
2.7s avg
Import
Disk
32MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v3.1.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.980 runs
installs and imports cleanly · install 0.0s · import 0.000s · 32.8MB
glibc
py 3.103.980 runs
installs and imports cleanly · install 2.7s · import 0.000s · 34MB
32MB installed
● package 32MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

variables
def test_example(variables):
The 'variables' fixture is automatically discovered and injected by pytest; no explicit Python import statement for 'variables' is needed in test files.

Create a variable file (e.g., `variables.json`) and then use the `--variables` command-line option with pytest. The variables will be available as a `variables` fixture in your tests. Environment variables can be used as fallbacks within the variable files.

# content of variables.json { "base_url": "https://api.example.com", "api_key": "${API_KEY:default_key}" } # content of test_api.py def test_api_endpoint(variables): assert variables['base_url'] == 'https://api.example.com' assert variables['api_key'] is not None # Example of accessing an environment variable fallback: import os expected_api_key = os.environ.get('API_KEY', 'default_key') assert variables['api_key'] == expected_api_key # To run: pytest --variables variables.json # You can also pass multiple files: pytest --variables file1.json --variables file2.yaml
Debug
Known issues
breakingVersion 3.1.0 dropped support for Python 3.7. Ensure your project uses Python 3.8 or newer.
fix
Upgrade Python to 3.8+ or pin `pytest-variables` to `<3.1.0`.
affects: >=3.1.0
breakingVersion 3.0.0 switched internal variable storage from `_variables` to `pytest.stash`. While not a public API, direct access to `_variables` will break.
fix
Avoid directly accessing internal plugin attributes. Use the `variables` fixture for all variable access.
affects: >=3.0.0
gotchaWhen multiple `--variables` files are specified, later files in the command line override values from earlier files if duplicate keys are encountered (for non-dictionary values). For dictionaries, they are merged.
fix
Be mindful of the order when specifying multiple variable files. Place more specific or overriding configurations later in the command-line argument list.
affects: *
gotchaIf using YAML files, the `yaml_loader` can be configured in `pytest.ini`. The loader name (e.g., `BaseLoader`, `SafeLoader`, `FullLoader`, `UnsafeLoader`) is case-sensitive. The default is `FullLoader`.
fix
Verify the `yaml_loader` setting in `pytest.ini` (e.g., `[pytest] yaml_loader = SafeLoader`) matches the expected case and loader type if encountering YAML parsing issues.
affects: *
Upgrade
Version history
3.1.0latest on PyPI · released Feb 1, 2024
Audit
Dependencies
pytestrequiredPeer dependency, as it is a pytest plugin.
hjsonoptionalRequired for Human JSON (.hjson) file support.
PyYAMLoptionalRequired for YAML (.yaml, .yml) file support.
tomloptionalRequired for TOML (.toml) file support.
Agent activity
11 hits · last 30 days
node
8
Resources
pytest-variables — pip install pytest-variables · libregistry