Python单元测试导入问题
我目前正在研究具有以下目录结构的Python应用程序。在这种情况下,app.py
imports mongodb_service.py
本地,这又参考secreds_manager_service.py.py
。
functions
__init__.py
mongodb_crud_service
__init__.py
app.py
mongodb_service.py
secrets_manager_service.py
我已经开始从项目根部编写测试:
tests
__init__.py
mongodb_crud_service_tests
__init__.py
unit
__init__.py
test_lambda.py
不幸的是,我无法从项目的根(python -m UnitSest Discover
)运行单元测试。 test_lambda.py
引用通过全局导入的服务模块(例如来自functions.mongodb_crud_service import import import app
)。每当我以这种方式运行单元测试时,我都会收到以下错误,表面上是由于本地导入所致:modulenotfounderror:no模块名为“ mongodb_service”
。有人有任何技巧吗?
我的应用程序是AWS SAM应用程序,因此它是这样的结构。
I am currently working on a Python application with the following directory structure. In this case, app.py
imports mongodb_service.py
locally, which in turn references secrets_manager_service.py
.
functions
__init__.py
mongodb_crud_service
__init__.py
app.py
mongodb_service.py
secrets_manager_service.py
I have begun writing tests from the project root:
tests
__init__.py
mongodb_crud_service_tests
__init__.py
unit
__init__.py
test_lambda.py
Unfortunately, I am unable to run unit tests from the root of my project (python -m unittest discover
). test_lambda.py
references the service modules via global imports (e.g. from functions.mongodb_crud_service import app
). Whenever I run the unit tests in this manner, I receive the following error, ostensibly due to the local imports: ModuleNotFoundError: No module named 'mongodb_service'
. Does anyone have any tips?
My application is an AWS SAM application, so it is structured like this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论