[英]Testing lambdas (Python) with aws-cdk?
I want to setup unit tests for my lambdas that are written in Python. 我想为用Python编写的lambda设置单元测试。 I am using aws-cdk to develop and deploy my lambdas.
我正在使用aws-cdk开发和部署我的lambda。 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'. 我当时正在研究库“ unittest”。 I set up a folder names 'test' and put a test file in there named: test_first.py.
我设置了一个名为“ test”的文件夹,并在其中放置了一个名为test_first.py的测试文件。 I could then execute all test files in this folder by using the command:
python -m unittest discover ./test
然后,我可以使用以下命令执行此文件夹中的所有测试文件:
python -m unittest discover ./test
test_first.py 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.
我现在遇到的问题是我不知道如何导入另一个python文件并在此文件中测试其方法。 This would be the lambda I have written and test its methods.
这将是我编写并测试其方法的lambda。 How do I do this?
我该怎么做呢?
Lambda python files are just like any other python files whose methods can be tested as unit tests. Lambda python文件与其他任何python方法都可以作为单元测试进行测试的python文件一样。 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. 您可以像导入my_sum中的 sum一样导入lambda方法,然后在其上写一些断言。 This document is a good read for the same.
该文档对于相同内容来说是不错的阅读。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.