{"id":6204,"library":"pytest-variables","title":"pytest-variables","description":"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.","status":"active","version":"3.1.0","language":"en","source_language":"en","source_url":"https://github.com/pytest-dev/pytest-variables","tags":["pytest","plugin","variables","configuration","testing","fixtures","json","yaml","toml","hjson"],"install":[{"cmd":"pip install pytest-variables","lang":"bash","label":"Base Installation"},{"cmd":"pip install pytest-variables[hjson]","lang":"bash","label":"With HJSON support"},{"cmd":"pip install pytest-variables[yaml]","lang":"bash","label":"With YAML support"},{"cmd":"pip install pytest-variables[toml]","lang":"bash","label":"With TOML support"}],"dependencies":[{"reason":"Peer dependency, as it is a pytest plugin.","package":"pytest","optional":false},{"reason":"Required for Human JSON (.hjson) file support.","package":"hjson","optional":true},{"reason":"Required for YAML (.yaml, .yml) file support.","package":"PyYAML","optional":true},{"reason":"Required for TOML (.toml) file support.","package":"toml","optional":true}],"imports":[{"note":"The 'variables' fixture is automatically discovered and injected by pytest; no explicit Python import statement for 'variables' is needed in test files.","symbol":"variables","correct":"def test_example(variables):"}],"quickstart":{"code":"# content of variables.json\n{\n  \"base_url\": \"https://api.example.com\",\n  \"api_key\": \"${API_KEY:default_key}\"\n}\n\n# content of test_api.py\ndef test_api_endpoint(variables):\n    assert variables['base_url'] == 'https://api.example.com'\n    assert variables['api_key'] is not None\n    # Example of accessing an environment variable fallback:\n    import os\n    expected_api_key = os.environ.get('API_KEY', 'default_key')\n    assert variables['api_key'] == expected_api_key\n\n# To run: pytest --variables variables.json\n# You can also pass multiple files: pytest --variables file1.json --variables file2.yaml\n","lang":"python","description":"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."},"warnings":[{"fix":"Upgrade Python to 3.8+ or pin `pytest-variables` to `<3.1.0`.","message":"Version 3.1.0 dropped support for Python 3.7. Ensure your project uses Python 3.8 or newer.","severity":"breaking","affected_versions":">=3.1.0"},{"fix":"Avoid directly accessing internal plugin attributes. Use the `variables` fixture for all variable access.","message":"Version 3.0.0 switched internal variable storage from `_variables` to `pytest.stash`. While not a public API, direct access to `_variables` will break.","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"Be mindful of the order when specifying multiple variable files. Place more specific or overriding configurations later in the command-line argument list.","message":"When 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.","severity":"gotcha","affected_versions":"*"},{"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.","message":"If 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`.","severity":"gotcha","affected_versions":"*"}],"env_vars":null,"last_verified":"2026-04-14T00:00:00.000Z","next_check":"2026-07-13T00:00:00.000Z","problems":[]}