Testing in a memoized python arround function

I am building a bunch of tests and found that some of them fail when they all work together. Looking at why they break, I found that it was because we remember some of the system calls to install.

Since I'm making fun of syscalls and not memoized, the function is evaluated only once and so it flows this information into subsequent tests.

What is the elegant way to work with memoized functions:

  • Discard these system calls
  • Create a parameter in memoize to not work for tests
  • Check memoize decorator to return parameters
  • Find where the data is stored and clear it at the end of each test.

Any other ideas?

thank

+3


source to share


3 answers


It might be too late, but I found it 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!

0


source


Try using MagicMock

vanilla instead Mock

.

Say your code looks something like this:

@memoize.Memoize()
def OAuthClient():
    """Returns an Oauth client."""
    return 'blah'

      

Mocks Mock

like this:

mymodule.OauthPasswordClient = mock.Mock()

      



Returns an error like this:

TypeError: unsupported operand type(s) for +=: 'int' and 'Mock'

      

But scoffs MagicMock

like this:

mymodule.OauthPasswordClient = mock.MagicMock()

      

Works well.

0


source


I think the first option might be the best.

  • Discard these system calls
-1


source







All Articles