Python单元测试导入问题

发布于 2025-02-13 08:51:37 字数 850 浏览 1 评论 0原文

我目前正在研究具有以下目录结构的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文