Testing Lambda HandlersΒΆ

The way awstin separates Lambda handlers into a parser and a main function promotes testing the function in parts as well.

The parser can be tested individually given example events, and asserting that the returned values are expected inputs to the main function.

def my_parser(event, context):
    return event["a"], event["b"]

@lambda_handler(my_parser)
def my_handler(a: int, b: str):
    ...

# -------

def test_parser():
    args = my_parser(test_event, test_context)
    assert ...

The main function can be tested directly as well. When a function my_handler is wrapped with awstin.awslambda.lambda_handler(), the returned function has an inner attribute containing the wrapped function.

def test_handler():
    result = my_handler.inner(1, "abc")
    assert ...