Python 单元测试 : Fail due to import from same folder

标签 python unit-testing testing import

这个问题看起来很微不足道,但我在网上找不到一个单一的答案。这是我的设置:

project:
- src:
- - __init__.py (empty)
- - file1.py
- - file2.py
- test:
- - test1.py
- - test2.py
- - __init__.py (empty)

现在,我要运行

python -m unittest discover

来自项目文件夹。

在 test1.py 中,我从我的第一个源文件导入和导入:

from src.file1 import class1

然而,在 file1.py 中,我从另一个导入:

from file2 import class2

如上所述运行单元测试会导致:

[...]
    from file2 import class2
ModuleNotFoundError: No module named 'file2'

我在这里做错了什么?

谢谢你,祝你有美好的一天!

最佳答案

您应该完全限定您的进口,即而不是:

from file2 import class2

使用

from .file2 import class2

注意 .from 之后。

更新:验证:

(dev) go|c:\srv\tmp\unttst\project> tree
.
|-- src
|   |-- __init__.py
|   |-- file1.py
|   `-- file2.py
`-- test
    |-- __init__.py
    `-- test1.py

2 directories, 5 files

(dev) go|c:\srv\tmp\unttst\project> type src\file1.py

from .file2 import Class2

class class1:
    pass

(dev) go|c:\srv\tmp\unttst\project> type src\file2.py


class Class2:
    pass

(dev) go|c:\srv\tmp\unttst\project> type test\test1.py

from src.file1 import class1

(dev) go|c:\srv\tmp\unttst\project> python -m unittest discover

----------------------------------------------------------------------
Ran 0 tests in 0.000s

OK

(dev) go|c:\srv\tmp\unttst\project>

请注意,我正在从 from src.xxx 有意义的目录中运行命令。

更新 2: 注意,Python 不适合直接在子目录中运行单个文件(即作为入口点)。 Guido 强烈反对这种用法,所以这不太可能改变。虽然您可以用自己的方式解决这个问题,但以正确的方式做到这一点非常简单,值得学习。

让我们首先更改 src/file1.py 使其具有 main() 函数:

(dev) go|c:\srv\tmp\unttst\project> cat src\file1.py

from .file2 import Class2

class class1:
    pass


def main():
    print("hello from main")

注意:我没有添加 if __name__=="__main__" 部分。

从命令行调用此函数的正确方法是使 project 成为“真正的”包。

“真正的”包是通过添加 setup.py 文件创建的。 setup.py 文件可以包含很多字段,但此用例所需的唯一字段如下:

(dev) go|c:\srv\tmp\unttst\project> cat setup.py

from setuptools import setup

setup(
    name="myproject",
    entry_points={
        'console_scripts': """
            run-file1 = src.file1:main
        """
    }
)

请注意 setup.py 位于 project 文件夹中。

接下来以“开发”模式安装包:

(dev) go|c:\srv\tmp\unttst\project> pip install -e .

注意最后的 .

setup.py 文件中的 entry_points .. console_scripts 现在已经为您创建了一个新的 shell 命令:

(dev) go|c:\srv\tmp\unttst\project> run-file1
hello from main

console_scripts 行

run-file1 = src.file1:main

表示创建一个名为 run-file 的 shell 命令,它应该执行 src.file1 中的 main 函数。

关于Python 单元测试 : Fail due to import from same folder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54793765/

相关文章:

unit-testing - 如何在 Go Test IntelliJ 中重复运行一个或一组测试

android - 如何在 Android 手机上测试 Android 应用程序并获取错误日志?

python - 基础 Python - 查找列表中元素之间的差异 - IndexError

Python SQLModel - 截断表或全部删除并获取行数

在单元测试期间控制定义的值

testing - 轴突框架 : How to test @EventHandler

performance - 哪个是自动 GUI 性能测试的最佳工具?

python - 如何自动删除 Pelican CMS 中的 "Powered by ..."?

Python - 仅在忽略大小写的字符串中计算字母数量

python - 单元测试实现集合接口(interface)的类?