python - 单元测试发现测试根目录时出现 ImportError

标签 python python-3.x unit-testing testing python-unittest

运行测试发现使用这些格式有效:

python -m unittest discover tests/folder1/folder2 -v
python -m unittest discover tests/folder1

当我在测试脚本的根目录上运行发现测试时会出现此问题。

python -m unittest discover tests  # raised ImportError

我的项目结构是这样的:

project_name/
    src/
        __init__.py
        # modules
    tests/
        folder1/
            __init__.py
            folder2/
                # test scripts
                __init__.py
        __init__.py

根据我在一些相关帖子中读到的内容,问题出在 PATH 上。但是我已经通过运行此命令包含了 src 路径,并且 ImportError 仍然发生。

export PYTHONPATH=$PYTHONPATH:$(pwd)/src

python 版本:3.5.0

最佳答案

在测试文件夹前添加 test_ 解决了这个问题。

关于python - 单元测试发现测试根目录时出现 ImportError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58054761/

相关文章:

python - 将 Pandas 列乘以多索引值

python - NumPy 的 : Calculating the average of values between two indices

python - Groupby - 如何将逻辑检查结果应用于所有行

javascript - 在 casperJs 测试中使用 jquery

java - csv 阅读器的 mockito 和单元测试

javascript - 在单元测试中使用 AudioContext

python - 在 h5py 中,类型 "|O"是什么?

python - 如何在Python中编写selenium循环?

python - 如何让 estimator.predict 预测一个样本

python - python中的asyncio.sleep()是如何实现的?