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 3.8+ or PyPy3

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 3.1.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 3.1.0
File Size Uploaded
pytest_variables-3.1.0.tar.gz 7.4 kB Details

Built distribution (wheel)

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

Total release size: 13.5 kB

Release files / pytest_variables-3.1.0.tar.gz

Download URL pytest_variables-3.1.0.tar.gz
Size 7.4 kB
Tags Source
SHA-256 checksum
How to use checksums
4719b07f0f6e5d07829b19284a99d9159543a2e0336311f7bc4ee3b1617f595d
BLAKE2b-256 checksum
How to use checksums
6552d756c9704a80119f2e8a84e418290f8bd1b7e1415c3417a1b9c13fcd2a87
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.7

Release files / pytest_variables-3.1.0-py3-none-any.whl

Download URL pytest_variables-3.1.0-py3-none-any.whl
Size 6.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4c864d2b7093f9053a2bed61e4b1d027bb26456924e637fcef2d1455d32732b1
BLAKE2b-256 checksum
How to use checksums
4bfe30dbeccfeafa242b3c9577db059019022cd96db20942c4a74ef9361c5b3c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.7

Release history Release notifications | RSS feed

This release

3.1.0 This release

2 release files

3.0.0

2 release files

2.0.0

2 release files

1.9.0

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