Skip to main content

pytest-variables

pytest-variables is a plugin for pytest that provides variables to tests/fixtures as a dictionary via a file specified on the command line.

License PyPI Travis Issues Requirements

Requirements

You will need the following prerequisites in order to use pytest-variables:

  • Python 2.7, 3.6, PyPy, or PyPy3

  • pytest 2.6 or newer

Installation

To install pytest-variables:

$ pip install pytest-variables

Additional formats

The following optional formats are supported, but must be explicitly installed as they require additional dependencies:

Human JSON

Human JSON is a configuration file format that caters to humans and helps reduce the errors they make. To install Human JSON support:

$ pip install pytest-variables[hjson]

YAML

YAML is a human friendly data serialization standard for all programming languages. To install YAML support:

$ pip install pytest-variables[yaml]

YAML Loader

You can specify which loader to use by setting yaml_loader in pytest.ini (or similar file) to one of the following:

  • BaseLoader

  • SafeLoader

  • FullLoader (default)

  • UnsafeLoader

[pytest]
yaml_loader = BaseLoader

Note that loader is case-sensitive.

To learn more about the loader, see here

TOML

TOML aims to be a minimal configuration file format that’s easy to read due to obvious semantics. TOML is designed to map unambiguously to a hash table. To install TOML support:

$ pip install pytest-variables[toml]

Contributing

We welcome contributions.

To learn more, see Development

Specifying variables

Use the –variables command line option one or more times to specify paths to files containing your variables:

$ pytest --variables firefox-53.json --variables windows-10.json

with the following contents for the firefox-53.json file:

{
  "capabilities": {
    "browser": "Firefox",
    "browser_version": "53.0"
  }
}

and another file named windows-10.json with:

{
  "capabilities": {
    "os": "Windows",
    "os_version": "10",
    "resolution": "1280x1024"
  }
}

you’ll get the merged version of your variables:

{
  "capabilities": {
    "browser": "Firefox",
    "browser_version": "53.0",
    "os": "Windows",
    "os_version": "10",
    "resolution": "1280x1024"
  }
}

If multiple files are specified then they will be applied in the order they appear on the command line. When duplicate keys with non dictionary values are encountered, the last to be applied will take priority.

Accessing variables

With a JSON variables file such as:

{
  "foo": "bar",
  "bar": "foo"
}

Specify the variables funcarg to make the variables available to your tests. The contents of the files are made available as a dictionary:

def test_foo(self, variables):
    assert variables['foo'] == 'bar'
    assert variables.get('bar') == 'foo'
    assert variables.get('missing') is None

Resources

Release files for pytest-variables 1.9.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytest-variables 1.9.0
File Size Uploaded
pytest-variables-1.9.0.tar.gz 8.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pytest-variables 1.9.0
File Interpreter ABI Platform
pytest_variables-1.9.0-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 14.5 kB

Release files / pytest-variables-1.9.0.tar.gz

Download URL pytest-variables-1.9.0.tar.gz
Size 8.8 kB
Tags Source
SHA-256 checksum
How to use checksums
f79851e4c92a94c93d3f1d02377b5ac97cc8800392e87d108d2cbfda774ecc2a
BLAKE2b-256 checksum
How to use checksums
935b4794afc0c069e7d2e58cc7da8469d49e5df75bdaeef5524121ccf39ad11d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.1

Release files / pytest_variables-1.9.0-py2.py3-none-any.whl

Download URL pytest_variables-1.9.0-py2.py3-none-any.whl
Size 5.7 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
ccf4afcd70de1f5f18b4463758a19f24647a9def1805f675e80db851c9e00ac0
BLAKE2b-256 checksum
How to use checksums
f6463b52bf7ac688321085da5b1237f815398af6694dc2c3f85ee4dce59b03e0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.1

Release history Release notifications | RSS feed

3.1.0

2 release files

3.0.0

2 release files

2.0.0

2 release files

This release

1.9.0 This release

2 release files

1.8.0

2 release files

1.7.1

2 release files

1.7.0

2 release files

1.6.1

2 release files

1.6.0

2 release files

1.5.1

2 release files

1.5.0

2 release files

1.4

2 release files

1.3

2 release files

1.2

1 release file

1.1

1 release file

1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page