pytest-variables

3.1.0 · active · verified Tue Apr 14

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.

Warnings

Install

Imports

Quickstart

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

view raw JSON →