python - Pytest 找不到 Lambda 层中定义的函数
问题描述
我最近在我的一个工作项目中添加了一个 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
依赖项.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 代码中的最佳方法是什么?
解决方案
推荐阅读
- javascript - Mongoose/MongoDB GeoJSON 查询返回空结果
- php - Wordpress:显示与 2 个术语相关的项目
- nearprotocol - Near Explorer block query starts from 9820221
- docker - 在 windows 容器中,如何优雅地关闭 asp.net core 应用程序?
- python - 为什么lambdify永远不会停止?
- jquery - 将选择更改为他的下一个值
- git - 让 git 在不覆盖现有代码的情况下进行更改
- c# - Open XML 删除 MS Word 表行损坏图像
- elasticsearch - 有没有办法使用弹性搜索数据源的当前日期计算时差?
- xml - 如何在 xsl 中实现附加赋值运算符,即“+=”