我是单元测试的新手,我正在尝试在 python 3 中创建我的第一个单元测试文件。所以我有以下非常基本的文件夹结构,只是模仿在线类(class)。
D:\Projects\Projects\unit_testing_course # parent dir absolute path
unit_testing_course/ # root/parent folder
first_project.py # has a function called avg()
tests/
test_first_project.py # I want to import avg() into this file
test_first_project.py 是我用于单元测试的文件,我想从它上面的父目录中存在的 first_project.py 导入 avg() 函数。
我在 Windows 10 上,我打开了以下路径的 git bash:'/d/Projects/Projects/unit_testing_course'(即包含 first_project.py 的父目录,我正在尝试导入的模块/函数到测试子目录中的 test_first_project.py 文件)。我一直在运行 python tests/test_first_project.py
我很快发现我不能使用类似 “from .. import first_project”或“from .. import avg”,这会导致“ValueError: attempted relative import beyond top-level package”错误消息。
因此,通过阅读文档,我了解到“来自”和“导入”都是用于模块或包的说明。 https://docs.python.org/3/tutorial/modules.html
"Python has a way to put definitions in a file and use them in a script or in an interactive instance of the interpreter. Such a file is called a module; definitions from a module can be imported into other modules or into the main module (the collection of variables that you have access to in a script executed at the top level and in calculator mode).
A module is a file containing Python definitions and statements. The file name is the module name with the suffix .py appended. Within a module, the module’s name (as a string) is available as the value of the global variable __name__."
所以听起来就 python 3 而言,每个 .py 文件都是一个模块,模块名本质上是 .py 文件名,末尾没有 .py 扩展名。听起来我正在尝试从 test_first_project.py 文件的父目录导入模块。
该文档链接似乎有助于解释当前目录中有一个模块的场景,您正在从中执行程序/python shell,而您只是试图将函数从该模块导入到该程序中。
但在这种情况下,我们似乎需要实际告诉 python 在特定目录(test_first_project.py 文件的父目录)中查找名为“first_project”的模块。我查看了模块搜索路径部分,https://docs.python.org/3/tutorial/modules.html#the-module-search-path
When a module named spam is imported, the interpreter first searches for a built-in module with that name. If not found, it then searches for a file named spam.py in a list of directories given by the variable sys.path. After initialization, Python programs can modify sys.path. The directory containing the script being run is placed at the beginning of the search path, ahead of the standard library path.
然后我尝试...
import sys
import os
p_dir_path = os.path.abspath('../')
sys.path.append(p_dir_path)
from first_project import avg
这仍然让我收到相同的错误消息“ValueError:尝试相对导入超出顶级包”。我希望以编程方式修改 sys.path 可以改变一切......
那么我实际上需要做什么才能从父目录导入模块? 或者...在 python 中不可能做到这一点吗?我是否需要使用包而不是模块(即我是否需要在我的父目录中创建一个 init.py)?
我在 stackoverflow 上看到过关于尝试将文件导入不同目录和其他情况的其他问题和帖子,但模仿这些问题的语法对我没有帮助,我认为我花了太多时间对此。我也确信我不是唯一一个想要创建单元测试并因导入语句、模块和包的问题而分心的人。
请您参考如下方法:
您应该为您的项目适本地设置您的 PYTHONPATH 变量。在 unit_testing_course 中,运行
export PYTHONPATH=$(pwd)
在你的壳里。并考虑以下代码:
first_project.py
def avg(l):
return sum(l)/len(l) if len(l) > 0 else 0
tests/test_first_project.py
import unittest
from first_project import avg
class Test(unittest.TestCase):
...
tests/__init__.py
from .test_first_project import *
运行 python -m unittest tests 将按预期工作(前提是 python 指向您所需的 python 版本)。您还可以使用 virtualenv 并修改其 venv/bin/activate 以自动执行此路径设置。当您的项目继续增长时,我建议在基础中有两个单独的目录 - 一个用于项目本身,一个用于测试。然后使用 relative imports 在每个分别。如果您选择 package,这将使您的项目变得灵活。它或将其用作 submodule或类似的东西。
