【发布时间】:2022-04-27 08:55:55
【问题描述】:
我最近在我的一个工作项目中添加了一个 Lambda 层,虽然它在几乎所有方面都取得了巨大的成功,但我现在在运行我的测试时遇到了问题。这是我精简的目录结构:
root
- dependencies-layer
- python
- __init__.py
- boto3
- (list of other modules)
- dependencies.py
- requirements.txt
- src
- hello_world
- __init__.py
- hello_world.py
- requirements.txt
- tests
- hello_world
- unit
- test_hello_world.py
- pytest.ini
- template.yaml
dependencies.py:
import boto3
def db_conn():
db = boto3.resource("dynamodb")
return db
hello_world.py
from dependencies import db_conn
def hw_handler():
(unimportant functionality)
test_hello_world.py
from src.hello_world import hello_world
(unimportant unit tests)
所有功能都很好用。运行 sam build 后,我可以在本地运行和触发 hw_handler,然后也可以部署和触发它,一切都没有问题。当我运行 pytest 并触发 test_hello_world.py 时,问题就出现了。我收到以下错误:
(Traceback)
src/hello_world/hello_world.py:1: in <module>
ImportError: cannot import name 'db_conn' from 'dependencies' (unknown location)
所以错误是当 pytest 导入要测试的 lambda 时,它无法从导入的 lambda 中的我的 lambda 层导入函数。显然 pytest 找不到我的依赖项文件夹,所以我的问题是,将 Lambda 层合并到我的 pytest 代码中的最佳方法是什么?
【问题讨论】:
标签: python aws-lambda pytest aws-sam aws-lambda-layers