Skip to main content

A pytest plugin for caching test results using pickle.

Project description

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.

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_pickle_cache-0.2.0.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

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

pytest_pickle_cache-0.2.0-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

Details for the file pytest_pickle_cache-0.2.0.tar.gz.

File metadata

  • Download URL: pytest_pickle_cache-0.2.0.tar.gz
  • Upload date:
  • Size: 3.9 kB
  • Tags: Source
  • Uploaded using 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}

File hashes

Hashes for pytest_pickle_cache-0.2.0.tar.gz
Algorithm Hash digest
SHA256 1967257ca43396b4abe663f2357f94316e56656c097b4c8d413c4884cd47a659
MD5 b4ad1f066284162c1537263ba7b3652f
BLAKE2b-256 8afea862bc1968cc05ee7f83f76de91185af5294a357343ceafbc179dc66f152

See more details on using hashes here.

File details

Details for the file pytest_pickle_cache-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: pytest_pickle_cache-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 5.0 kB
  • Tags: Python 3
  • Uploaded using 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}

File hashes

Hashes for pytest_pickle_cache-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 02de80ea372bb78063a9caefec6082ecac3537b96a48edee0c8ac0e6abf5c25e
MD5 7bd0f75bf01f25e850fdbe942807201f
BLAKE2b-256 e8af8f846f0088920542e335c8eeef5bc77cf360659cedea356bf184fbd98af5

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