Skip to main content

Bust functools.lru_cache when running pytest to avoid test pollution

Project description

pytest-antilru

Build Status Coverage Status license supported python versions pypi wheels pypi development status

Caching expensive function calls with functools.lru_cache is simple and great performance optimization. It works so well that it'll even speed up your unit test runs! Too bad it violated test isolation and caches the wrong values under test conditions, introducing test pollution (persisted state between test runs). This package will bust the lru_cache between test runs, avoiding test pollution and helping you keep your sanity.

Imagine you mock a network call out and your application ends up caching these mocked results:

def expensive_network_call() -> int:
    # Pretend this is an expensive network call.
    # You want to cache this for performance but you want to run tests with different responses as well.
    return 1


@lru_cache()
def cache_me() -> int:
    return expensive_network_call()

Now you have test pollution:

def test_a_run_first() -> None:
    assert cache_me() == 1


def test_b_run_second() -> None:
    # We want to mock the network call for this test case
    with mock.patch.object(sys.modules[__name__], 'expensive_network_call', return_value=2) as mock_network_call:
        assert cache_me() == 2
        assert mock_network_call.called

On your next test run, it doesn't matter what you mock, the results are already cached. Now trying running those two test out-of-order sequence and tell me how it goes.

Dependencies

Since this is a pytest plugin, you need to be using pytest to run your tests.

This project is python 2.7 and python 3.5, 3.6, 3.7 compatible.

Installation

Simply install this in the same python environment that pytest uses and the rest is magic.

pip install pytest-antilru

How to test the software

make test

Credits and references

This project was a re-engineering of a similar project a colleague of mine wrote. That project was not intended to be open-source and rather than go though all the hoops and hurdles to sanitize it, I've written it from the ground up such that it's kosher to open-source (given that it's such as small project).

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-antilru-1.0.5.tar.gz (3.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_antilru-1.0.5-py2.py3-none-any.whl (4.7 kB view details)

Uploaded Python 2Python 3

File details

Details for the file pytest-antilru-1.0.5.tar.gz.

File metadata

  • Download URL: pytest-antilru-1.0.5.tar.gz
  • Upload date:
  • Size: 3.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.5

File hashes

Hashes for pytest-antilru-1.0.5.tar.gz
Algorithm Hash digest
SHA256 ec999a7c3428d6ddafe86d4cd800008dcc0fcc61066312afdc90eee81dd4fdcc
MD5 3079e1bc17c84e1239c1633ffc5f125a
BLAKE2b-256 d503d6404d7b415b59bb0f50b02d86663cf3f3f46577857470169fe537512d91

See more details on using hashes here.

File details

Details for the file pytest_antilru-1.0.5-py2.py3-none-any.whl.

File metadata

  • Download URL: pytest_antilru-1.0.5-py2.py3-none-any.whl
  • Upload date:
  • Size: 4.7 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.5

File hashes

Hashes for pytest_antilru-1.0.5-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 bfd61ea3bf7faf2158673bd9b50dbf2a22efaa7ff6eb9f301703f749adcf0e9e
MD5 ecc62c9682bbce700dcf0640ad2acc92
BLAKE2b-256 bc25cdcdf3de3a20b45c937e5c17dacda80bc1382b016046db36a66df50859bc

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