Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing in python arround memoized functions

Tags:

python

testing

I'm building a bunch of tests and discovered that some of them are failing when they are all run together. Looking at why they are breaking I discovered it is because we memoize some of the system calls for the setup.

Because I'm mocking the system calls and not the memoized the function is only computed once and so it leaks that information into the subsequent tests.

What is the elegant way to deal with memoized functions:

  • Mock those instead of the system calls
  • Create a parameter on the memoize to not work for tests
  • Mock the memoize decorator to return the parameters
  • Find where the data is memoized and clean it at the end of each test

Any other ideas ?

Thanks

like image 222
user1634074 Avatar asked Sep 24 '26 15:09

user1634074


1 Answers

Maybe it's too late but I found very easy to use delete_memoized:

# your function
@memoize(timeout=50)
def your_long_function():
    # do stuff

# your test
def test_get_folder_content_error_handled(self, flf):
    delete_memoized(your_long_function)
    your_long_function()

I just used this solution in Django (using django-memoize) and it works like a charm!

like image 89
greenkey Avatar answered Sep 27 '26 05:09

greenkey