Skip to main content

Pytest plugin to load resource files relative to test code and to expect values to match them.

Project description

pytest-respect

Pytest plugin to load resource files relative to test code and to expect values to match them. The name is a contraction of resources.expect, which is frequently typed when using this plugin.

Motivation

The primary use-case is running tests over moderately large datasets where adding them as constants in the test code would be cumbersome. This happens frequently with integration tests or when retrofitting tests onto an existing code-base.

Examples

Json Data

The absolute simplest example is the following test:

def test_computation(resources):
    """Running compute on input.json creates an output matching output.json"""
    input = resources.load_json("input")
    output = compute(input)
    resources.expect_json(output, "output")

If it is found in a file called foo/test_stuff.py, then it will load the content of foo/test_stuff/test_computation__input.json, run the compute function on it, and assert that the output exactly matches the content of the file foo/test_stuff/test_computation__output.json.

Pydantic Models

With the optional pydantic extra, the same can be done with pydantic data if you have models for your input and output data:

def test_computation(resources):
    """Running compute on input.json creates an output matching output.json"""
    input = resources.load_pydantic(InputModel, "input")
    output = compute(input)
    resources.expect_pydantic(output, "output")

Installation

Install with your favourite package manager such as:

  • pip install pydantic-respect
  • poetry add --dev pydantic-respect
  • uv add --dev pydantic-respect

See your package management tool for details, especially on how to install optional extra dependencies.

Extras

The following extra dependencies are required for additional functionality:

  • poetry - load, save, and expect pydantic models of arbitrary data through type adapters.
  • numpy - convert numpy arrays and scalars to python equivalents when converting to JSON
  • jsonyx - alternative JSON encoder for semi-compact files, numeric keys, trailing commas, etc.

The resources Fixture

The main entry point to the library is the resources fixture. Each method is fully documented in-line so here we will discuss clusters of functionality and list the members.

Path Makers

TBD

resources.load_foo

TBD

resources.save_foo

TBD

resources.delete_foo

TBD

resources.expect_foo

TBD

Utilities

TBD: path, dir, json_to_text

JSON Enocders

TBD


Development

Installation

  • Install uv
  • Run uv sync --all-extras
  • Run pre-commit install to enable pre-commit linting.
  • Run pytest to verify installation.

Testing

This is a pytest plugin so you're expected to know how to run pytest when hacking on it. Additionally, scripts/pytest-extras runs the test suite with different sets of optional extras. The CI Pipelines will go through an equivalent process for each Pull Request.

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_respect-0.1.0.tar.gz (58.8 kB view details)

Uploaded Source

Built Distribution

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

pytest_respect-0.1.0-py3-none-any.whl (10.4 kB view details)

Uploaded Python 3

File details

Details for the file pytest_respect-0.1.0.tar.gz.

File metadata

  • Download URL: pytest_respect-0.1.0.tar.gz
  • Upload date:
  • Size: 58.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.4

File hashes

Hashes for pytest_respect-0.1.0.tar.gz
Algorithm Hash digest
SHA256 189ad5adc23a52baac6319826d44a0e5f5cd47b6cc8e960635ffe401e0934ae3
MD5 a7fc1d3c4db298b90d27d338e71aabe2
BLAKE2b-256 82bd135a2bb7e492d799d4aa31f14fad27e91d5da3ea4276b76cf80e6e588eb0

See more details on using hashes here.

File details

Details for the file pytest_respect-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pytest_respect-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 abe84ce85c74b985a107909863d2b4f9aea85dc5e4fc4c01e9a9ae526b57a699
MD5 1b4110508769f7a43a4eec75e04e91ed
BLAKE2b-256 de3f841d95b82e5cd02b2a667f3b2ef20cc18ce3846aeaa9a3cd46d2b16fe316

See more details on using hashes here.

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