Skip to main content

pytest-pickle-cache

PyPI Version Python Version Build Status Coverage Status

Overview

pytest-pickle-cache is a pytest plugin for caching test results using pickle. By utilizing this plugin, you can reduce test execution time and perform tests more efficiently.

Installation

You can install pytest-pickle-cache using the following command:

pip install pytest-pickle-cache

Fixture

The use_cache fixture is a pytest fixture that provides a caching mechanism for pytest, allowing you to store and retrieve objects using a specified key. The objects are serialized and deserialized using pickle and base64 encoding.

def use_cache(key: str, func: Callable[[], Any]) -> Any:
    """Retrieve a cached result or execute the function if not cached.

    Args:
        key (str): The key to identify the cached result.
        func (Callable[[], Any]): The function to execute if the result is
            not cached. The result of the function is serialized and stored
            in the cache for future use.

    Returns:
        Any: The cached result or the result of the executed function.
    """

Example

Here is a specific example of how to use pytest-pickle-cache to cache test results.

import datetime

import pytest
from pandas import DataFrame


def create() -> DataFrame:
    """Create a DataFrame with the current time."""
    now = datetime.datetime.now()
    return DataFrame({"now": [now]})


def test_create(use_cache):
    """Create a DataFrame using cache and compare the results."""
    # Retrieve DataFrame using cache
    df_cached = use_cache("key", create)

    # Create a new DataFrame
    df_created = create()

    # Assert that the cached DataFrame and the newly created DataFrame are different.
    assert not df_created.equals(df_cached)


def test_create_with_cache(use_cache):
    """Use cache to retrieve the same DataFrame and ensure the results are the same."""
    # Cache the DataFrame on the first call
    df_cached_first = use_cache("key", create)

    # Call the same function again to retrieve from cache
    df_cached_second = use_cache("key", create)

    # Assert that the cached DataFrame is the same on the second call.
    assert df_cached_first.equals(df_cached_second)

You can also use use_cache fixture as a fixture in your test file.

@pytest.fixture
def df(use_cache):
    return use_cache("key", create)

You can also use use_cache fixture with a parametrized fixture.

def create(param: int) -> DataFrame:
    """Create a DataFrame with the current time."""
    now = datetime.datetime.now()
    return DataFrame({"now": [now], "param": [param]})


@pytest.fixture(params=[1, 2, 3])
def df(use_cache, request):
    return use_cache(f"key_{request.param}", lambda: create(request.param))

Benefits

  • Efficiency in Testing: By using pytest-pickle-cache, you can avoid running the same test multiple times, reducing the overall test execution time.

  • Consistency of Results: Using cache ensures that you get the same result for the same input, maintaining consistency in your tests.

Metadata

Release files for pytest-pickle-cache 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytest-pickle-cache 0.2.0
File Size Uploaded
pytest_pickle_cache-0.2.0.tar.gz 3.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pytest-pickle-cache 0.2.0
File Interpreter ABI Platform
pytest_pickle_cache-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 8.9 kB

Release files / pytest_pickle_cache-0.2.0.tar.gz

Download URL pytest_pickle_cache-0.2.0.tar.gz
Size 3.9 kB
Tags Source
SHA-256 checksum
How to use checksums
1967257ca43396b4abe663f2357f94316e56656c097b4c8d413c4884cd47a659
BLAKE2b-256 checksum
How to use checksums
8afea862bc1968cc05ee7f83f76de91185af5294a357343ceafbc179dc66f152
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.10.0 {"installer":{"name":"uv","version":"0.10.0","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / pytest_pickle_cache-0.2.0-py3-none-any.whl

Download URL pytest_pickle_cache-0.2.0-py3-none-any.whl
Size 5.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
02de80ea372bb78063a9caefec6082ecac3537b96a48edee0c8ac0e6abf5c25e
BLAKE2b-256 checksum
How to use checksums
e8af8f846f0088920542e335c8eeef5bc77cf360659cedea356bf184fbd98af5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.10.0 {"installer":{"name":"uv","version":"0.10.0","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page