Pytest 无法正确导入文件夹

问题描述 投票:0回答:1

我正在尝试使用 pytest 为文件编写单元测试。我的目录结构如下所示:

--app
    --app.py
    --src
        --helpers
            --math
                --__init__.py
                --file1.py
                --file2.py

    --tests
        --unit
            --test_file1.py
        --functional
    --conftest.py

在 test_file1.py 中,我有以下img导入语句: 从 src.helpers.math.file1 导入 mainClass

当我在 test_file1.py 上调用 pytest 时,它坚持 ModuleNotFoundError:没有名为 helpers 的模块。我不明白为什么它找不到帮助者。

我尝试向 src 和 helpers 添加一个

__init__.py
文件;我尝试更改导入语句的调用方式,使其仅显示 src、helpers.math 和其他组合;但无论我做什么, pytest 似乎都找不到这个模块,我不明白为什么。另一个 pytest 文件正在测试仅包含函数而没有类的文件,运行良好,因此我确信我在类方面做错了。

pytest directory-structure
1个回答
0
投票

也许你应该尝试像这样导入你的文件

import helpers.math.file1

并使用功能坚持您的文件

helpers.math.file1.your_function
© www.soinside.com 2019 - 2024. All rights reserved.