A pytest plugin for caching test results using pickle.
Project description
pytest-pickle-cache
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pytest_pickle_cache-0.1.2.tar.gz.
File metadata
- Download URL: pytest_pickle_cache-0.1.2.tar.gz
- Upload date:
- Size: 4.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.21
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5329bdcec776bf7f70e49e9eecbbfebc1e70fdb52becaba55d3487f10138eb64
|
|
| MD5 |
a2c44218052df01655179eb3485fe4a2
|
|
| BLAKE2b-256 |
3b32e2dd581c1a0ea7059dc86d0950b4cb84e7d9d1a74b90b509095c4c5e1db1
|
File details
Details for the file pytest_pickle_cache-0.1.2-py3-none-any.whl.
File metadata
- Download URL: pytest_pickle_cache-0.1.2-py3-none-any.whl
- Upload date:
- Size: 5.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.21
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
963a2e02d1b219852d410d51acf309071fa2d58a9b1c945793557fab48176c6c
|
|
| MD5 |
ae95c190eec927327e09ce3932596249
|
|
| BLAKE2b-256 |
5c2655a3ec0026586ebefc0bfc5df64e81b24e2e9c388021582fe871c09c910a
|