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:

- title: name1
  description: description
  input: |-
    input content
  expected: |-
    expected output content
- title: name2
  description: 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.4.0.tar.gz (9.8 kB view details)

Uploaded Source

Built Distribution

pytest_param_files-0.4.0-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pytest_param_files-0.4.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.4.0.tar.gz
Algorithm Hash digest
SHA256 fac3c6a4cd3be0340ba77f438d5b0b3056e1942f6b3f11ddea6b607c1fcbf6b6
MD5 b9096881808bef292796e47c0bca30f7
BLAKE2b-256 df6ab1573ff61fc01c8edc6c52590a02ea6266a41e2b7be99ac9f65707617dd8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pytest_param_files-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0d28b39312bcf58d10237121b31762e8dc6a23d0157208324246b8805df76bdf
MD5 57299156399c69b98807b74ffd014752
BLAKE2b-256 820cc40c85e13793e7f42fb2e06194001992b24abe2d91e406234c6a4a447527

See more details on using hashes here.

Supported by

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