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 of this Example

  • 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.1.1.tar.gz (4.6 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.1.1-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pytest_pickle_cache-0.1.1.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.21

File hashes

Hashes for pytest_pickle_cache-0.1.1.tar.gz
Algorithm Hash digest
SHA256 98591deedf94594dee263322c7a81e9a1530103cbf6408fc83c8726dfbd14709
MD5 ffc604fbf37c443e352ada6e987401b0
BLAKE2b-256 9d3dd9ec8117c1a2789120a733f5b20cf9abb34a9dc9036a7ca808d0508049e2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pytest_pickle_cache-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 bb645c7664eb653789aff57db3f00755350bc67c2f837fdaf8fa653e8b7648f3
MD5 96975add76f3491a3a8cacbe10eaec7a
BLAKE2b-256 b800ef0f8f0f025d6bee0ddd5dad8cb9af4ff4e02ee524530d44ec7473f659ae

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