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.0.tar.gz (4.5 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.0-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for pytest_pickle_cache-0.1.0.tar.gz
Algorithm Hash digest
SHA256 1cd58a29a184ba8c252cd941c4766ad0d818d451658f42d1cf0aaa325dc552ad
MD5 b8442fa8905d5e92a8375a543e07adc8
BLAKE2b-256 c676ee7b8efc0f2dfaff8b6810c5ed58bd4e3a0c7ac0db9f4694385fc68766d9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pytest_pickle_cache-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 07985b527ea1dd2efa2cb8d669b64d4067f52159dda917bbb796984873cb8557
MD5 ce95c2db09242500f57f22d8577cb5c3
BLAKE2b-256 41dd1d3c469c0fbc24b76ca51074bdb3674245af44cdc61bf04e95417a9b9ec0

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