Skip to main content

A pytest plugin that generates unit test scenarios from data files.

Project description

pytest-scenario-files

Making Pytest Scenarios Easy and Scalable

PyPI pyversions Pytest

Hatch project Ruff Pre-Commit

pytest-scenario-files is a pytest plugin that runs tests using data loaded from files via parameterized fixtures.


Introduction

pytest has a feature called parameterization that allows you to run the same test function repeatedly using different inputs to test multiple scenarios. However, managing the test data for parameterization can be a problem. Sometimes the input data is very large or there are many test cases, so that it is impractical to put all of the data into the source code of the test.

This plug-in loads the data for the test scenarios from separate data files that are automatically matched up against the test functions. Each function can have one or more data files associated with it, and each file can contain multiple scenarios. The data files can be in JSON or YAML format.

Features

  • Loads data for scenarios from files into fixtures
  • Multiple scenario data sets may be in one file
  • There may be multiple data files for each test
  • Fixtures may refer to fixtures in other files
  • Can specify indirect parameterization
  • Intuitive and sane data file structure
  • Integration with Responses
  • NEW - Can load native Responses files
  • Integration with Respx

Compatibility

This package is a plug-in for pytest and works with Python 3.9 and up.

  • Tested with pytest versions 7.4, 8.2, 8.3, 8.4, and 9.0.
  • Tested with CPython 3.9-3.14 (including 3.14t).

Documentation

For more information on advanced usages, read the detailed documentation.


Quickstart

Installation

Install pytest-scenario-files from PyPI by using pip :

$ pip install pytest-scenario-files

Add Fixtures

For each test where you want to have data loaded from a file, add the name of a fixture to the list of arguments for the test. For instance, if you have a target function:

def foo(x):
    return 3 * x

And a test function:

def test_foo():
    assert foo(17) == 51

You can set up test_foo() for scenarios by adding two fixtures:

def test_foo(input_value, expected_result):
    assert foo(input_value) == expected_result

Create a Data File

A data file will be loaded if it matches all of the following criteria:

  1. The filename starts with data_, followed by the name of the test function with the prefix test_ removed. The remainder of the filename may be any value, and is usually used to identify the tests contained in the file.
  2. The filename must end in .json, .yaml, or .yml.
  3. The file is contained in a folder at or below the file that contains the test.

For this example, create a YAML file with the name data_foo_scenarios_1.yaml.

Inside the file, set up a structure with two levels of nested dictionaries.

  1. The top level is the name of each scenario.
  2. The second level has the name of each fixture with value assignments.
scenario_1:
  input_value: 17
  expected_result: 51
scenario_2:
  input_value: 7
  expected_result: 21
scenario_string:
  input_value: a
  expected_result: aaa
scenario_list:
  input_value:
    - x
  expected_result:
    - x
    - x
    - x

Run the Tests

$ pytest -vv -rA

You will see the test run four times, each with a different input value and expected result. (The option -vv gives verbose output; the option -rA prints all logging and output from the tests. Neither is necessary, but they're nice to have.)


Reporting Issues

If you encounter any problems, please file an issue including a detailed description and (if possible) an example of the problem.

License

Distributed under the terms of the MIT license, pytest-scenario-files is free and open source software.

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_scenario_files-1.3.1.tar.gz (38.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytest_scenario_files-1.3.1-py3-none-any.whl (12.1 kB view details)

Uploaded Python 3

File details

Details for the file pytest_scenario_files-1.3.1.tar.gz.

File metadata

  • Download URL: pytest_scenario_files-1.3.1.tar.gz
  • Upload date:
  • Size: 38.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pytest_scenario_files-1.3.1.tar.gz
Algorithm Hash digest
SHA256 2fb0283093c5b631f6c7ab21a1c4a0c19fb85fe0539ba36975c22829c7ad0e39
MD5 58825a25fa42addf39c8c20bd5c01e27
BLAKE2b-256 3240f4cfdcb84da171970343c8d958aa2cd5dcbc9c1ce2469fe4cef189e1e3e6

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytest_scenario_files-1.3.1.tar.gz:

Publisher: publish-release.yml on paulsuh/pytest-scenario-files

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pytest_scenario_files-1.3.1-py3-none-any.whl.

File metadata

File hashes

Hashes for pytest_scenario_files-1.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f45cfdf5e71e89baf0c907fc90eadc8332633bdebfa01df86331056de0426986
MD5 c0416b21a545d0ae100d587ac959930b
BLAKE2b-256 1d7ac3b32db5ad5d095f61c3130493c0856ca595dc14f6f743411e75640537ae

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytest_scenario_files-1.3.1-py3-none-any.whl:

Publisher: publish-release.yml on paulsuh/pytest-scenario-files

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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