Project Structure
MyLambda/
├── __init__.py
├── lambda_function.py
└── test
├── __init__.py
└── test_my_lambda_test_lambda_function.py
__init__.py
are empty files that mark the directories as python package so that they can be imported.
We will create a simple lambda_function.py
that simply returns id as response.
We have a test corresponding to the test file MyLambda/test/test_lambda_function.py
:
We will use pytest
to run our test:
$ pytest -v -s --cov=.
MyLambda/test/test_my_lambda_test_lambda_function.py::test_lambda_handler PASSED
--------- coverage: platform darwin, python 3.10.10-final-0 ----------
Name Stmts Miss Cover
--------------------------------------------------------------------------
MyLambda/__init__.py 0 0 100%
MyLambda/lambda_function.py 4 0 100%
MyLambda/test/__init__.py 0 0 100%
MyLambda/test/test_my_lambda_test_lambda_function.py 7 0 100%
--------------------------------------------------------------------------
TOTAL 11 0 100%
As our lambda function gets complex, we must explore additional functionality to test our lambda function. We can use patch
to mock resources like environment variables, etc
Our updated test
becomes
A fixture is a function that returns a test resource to supply a mock context
object to our lambda function. Let's rewrite our test function
In our example, we define a event
fixture that return dictionary. We then pass this fixture as an argument to our test function.