I want to setup unit tests for my lambdas that are written in Python. I am using aws-cdk to develop and deploy my lambdas. How do I propperly setup these unit tests? Which libraries do I use? How to put it in a packet structure? Maybe an example can clarify.
I was looking into the library 'unittest'. I set up a folder names 'test' and put a test file in there named: test_first.py. I could then execute all test files in this folder by using the command: python -m unittest discover ./test
test_first.py
import unittest
class FirstTest(unittest.TestCase):
def test_default(self):
self.assertEqual(10, (5+5))
if __name__ == '__main__':
unittest.main()
Is this the right way to do this? I now have the problem that I don't know how to import another python file and test its methods in this file. This would be the lambda I have written and test its methods. How do I do this?
Lambda python files are just like any other python files whose methods can be tested as unit tests. Below is a simple example
import unittest
from my_sum import sum
class TestSum(unittest.TestCase):
def test_list_int(self):
"""
Test that it can sum a list of integers
"""
data = [1, 2, 3]
result = sum(data)
self.assertEqual(result, 6)
if __name__ == '__main__':
unittest.main()
You can import your lambda method like we imported sum from my_sum and then write some asserts on it. This document is a good read for the same.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.