Skip to main content

Create pytest parametrize decorators from external files.

Project description

pytest-param-files

PyPI

A small package to generate parametrized pytests from external files.

Simply create a text file with an available format:

dot format (default):

[name1] description
.
input content
.
expected output content
.

[name2] description
.
input content
.
expected output content
.

yaml format:

name1:
  description: optional description
  input: |-
    input content
  expected: |-
    expected output content
name2:
  description: optional description
  input: |-
    input content
  expected: |-
    expected output content

Then, use the param_file pytest marker to create a parametrized test:

from pathlib import Path
import pytest

import my_function

PATH = Path(__file__).parent.joinpath("test_file.txt")

@pytest.mark.param_file(PATH, fmt="dot")
def test_function(file_params):
    assert my_function(file_params.content) == file_params.expected

and the output will be:

$ pytest -v test_file.py
...
test_file.py::test_function[1-name1] PASSED
test_file.py::test_function[8-name2] FAILED

Alternatively use the assert_expected method, which will can handle more rich assertion features:

@pytest.mark.param_file(PATH, fmt="dot")
def test_function(file_params):
    actual = my_function(file_params.content)
    assert file_params.assert_expected(actual, rstrip=True)
$ pytest -v test_file.py
...
test_file.py::test_function[1-name1] PASSED
test_file.py::test_function[8-name2] FAILED
...
E       AssertionError: Actual does not match expected
E       --- /path/to/test_file.txt:8
E       +++ (actual)
E       @@ -1 +1 @@
E       -content
E       +other

Installation

Install from PyPI:

$ pip install pytest-param-files

or install locally (for development):

$ pip install -e .

Regenerating expected output on failures

Running pytest with the --regen-file-failure option will regenerate the parameter file with actual output, if any test fails.

Note, currently regeneration of YAML files may not provide the same formatting as the original file, and will not preserve comments.

Other formats

TODO ...

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_param_files-0.5.0.tar.gz (9.8 kB view details)

Uploaded Source

Built Distribution

pytest_param_files-0.5.0-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

Details for the file pytest_param_files-0.5.0.tar.gz.

File metadata

  • Download URL: pytest_param_files-0.5.0.tar.gz
  • Upload date:
  • Size: 9.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.28.1

File hashes

Hashes for pytest_param_files-0.5.0.tar.gz
Algorithm Hash digest
SHA256 53f51e6771d99311c23f4257176d57291b260b40e89f36879af30041f61e9a67
MD5 72226c8c513571bef58e686b2a2c3afc
BLAKE2b-256 edd4c51aba31f7abb380a208c872ccd4a95de36aa3897a44d4f58b9c12e42183

See more details on using hashes here.

Provenance

File details

Details for the file pytest_param_files-0.5.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pytest_param_files-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 38d43ff9d643167d09ed593c28e7ce436c3b6344c6bdf034ff99ec67f2f29fb2
MD5 bfbe6bbc951e0f4db96abf9263e47711
BLAKE2b-256 4277917f26faee0207481c5fb96562f5712247965869faa9651426cfb38c311b

See more details on using hashes here.

Provenance

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