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.

Files for pytest-antilru, version 1.0.5
Filename, size File type Python version Upload date Hashes
Filename, size pytest_antilru-1.0.5-py2.py3-none-any.whl (4.7 kB) File type Wheel Python version py2.py3 Upload date Hashes View hashes
Filename, size pytest-antilru-1.0.5.tar.gz (3.6 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page