Skip to main content

pytest plugin for providing variables to tests/fixtures

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest_variables-3.1.0.tar.gz (7.4 kB view details)

Uploaded Source

Built Distribution

pytest_variables-3.1.0-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

Details for the file pytest_variables-3.1.0.tar.gz.

File metadata

  • Download URL: pytest_variables-3.1.0.tar.gz
  • Upload date:
  • Size: 7.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.7

File hashes

Hashes for pytest_variables-3.1.0.tar.gz
Algorithm Hash digest
SHA256 4719b07f0f6e5d07829b19284a99d9159543a2e0336311f7bc4ee3b1617f595d
MD5 e9119eda4607d8d705137016ae8cbff3
BLAKE2b-256 6552d756c9704a80119f2e8a84e418290f8bd1b7e1415c3417a1b9c13fcd2a87

See more details on using hashes here.

File details

Details for the file pytest_variables-3.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pytest_variables-3.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4c864d2b7093f9053a2bed61e4b1d027bb26456924e637fcef2d1455d32732b1
MD5 b9140e773a16f9b6b56e3ef19e5963f1
BLAKE2b-256 4bfe30dbeccfeafa242b3c9577db059019022cd96db20942c4a74ef9361c5b3c

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page