sloev/python-lambdarest

strange behaviour when unittesting with singleton lambda handler

sloev opened this issue · 1 comments

sloev commented

when unittesting code like the following

from lambdarest import lambda_handler

@lambda_handler.handle("get")
def my_own_get(event):
    return {"this": "will be json dumped"}

The lambda_handler singleton will stay instantiated between testcases since the module is not torn down completely (unittest does this to be able to later on gather the test results see https://chrisbailey.blogs.ilrt.org/2013/05/19/pythons-leaky-testcase-aka-hidden-gotchas-using-self/)

This means that the same lambda_handler instance will be used for all testcases, them being in different files or not. Which again means that the internal lookup http_methods will be scrampled.

A fix for this is to either put your import statements locally in your test functions like:

class test(unittest.TestCase):
    def test_this(self):
        from my_module import lambda_handler
        lambda_handler.handle("get")

or stop using the singleton and instantiate your own like:

from lambdarest import create_lambda_handler
lambda_handler = create_lambda_handler()
sloev commented

issue has been mentioned in README