单元测试的 Python 相对路径 [英] Python relative paths for unit tests

查看:32
本文介绍了单元测试的 Python 相对路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定目录结构:

/home/user/python/mypacakge/src/foo.py
/home/user/python/mypacakge/tests
/home/user/python/mypacakge/tests/fixtures
/home/user/python/mypacakge/tests/fixtures/config.json.sample
/home/user/python/mypacakge/tests/foo_tests.py
/home/user/python/mypacakge/README.md

其中 src 包含源代码,而 test 包含单元测试,我如何设置包"以便我在位于 test/ 中的单元测试中使用的相关导入可以加载src/?

Where src contains the source code, and test contains the unit tests, how do I setup a "package" so that my relative imports that are used in the unit tests located in test/ can load classes in src/?

类似问题:Python 相对导入和包Python:没有包或模块的相对导入,但第一个并没有真正回答我的问题(或者我不明白),第二个依赖符号链接(分别)将其破解.

Similar questions: Python Relative Imports and Packages and Python: relative imports without packages or modules, but the first doesn't really answer my question (or I don't understand it) and the second relies on symlinks to hack it together (respectively).

推荐答案

我想通了.

您必须在 每个 文件夹中都有 __init__.py,如下所示:

You have to have __init__.py in each of the folders like so:

/home/user/python/mypackage/src/__init__.py
/home/user/python/mypackage/src/Foo.py
/home/user/python/mypackage/tests
/home/user/python/mypackage/tests/fixtures
/home/user/python/mypackage/tests/fixtures/config.json.sample
/home/user/python/mypackage/tests/foo_test.py
/home/user/python/mypackage/tests/__init__.py
/home/user/python/mypackage/README.md
/home/user/python/mypackage/__init__.py

这告诉 python 我们有一个包"每个目录,包括顶级目录.所以,在这一点上,我有以下包:

This tells python that we have "a package" in each of the directories including the top level directory. So, at this point, I have the following packages:

mypackage
mypackage.test
mypackage.src

因此,因为python只会向下进入"目录,我们必须从最顶层包的根目录执行单元测试,在这种情况下是:

So, because python will only go "down into" directories, we have to execute the unit tests from the root of the top-most package, which in this case is:

/home/user/python/mypackage/

因此,从这里开始,我可以执行 python 并告诉它执行 unittest 模块,然后通过使用 命令行选项

So, from here, I can execute python and tell it to execute the unittest module and then specify which tests I want it to perform by specifying the module using the command line options

python -m unittest tests.foo_test.TestFoo

这告诉python:

  1. 执行python并加载模块unittest
  2. 告诉单元测试运行 TestFoo 类中包含的测试,该类位于 test 目录中的文件 foo_test.py 中.

Python 能够找到它,因为每个目录中的 __init__.py 将它们提升为 python 和 unittest 可以使用的包.

Python is able to find it because __init__.py in each of these directories promotes them to a package that python and unittest can work with.

最后,foo_test.py 必须包含一个像这样的导入语句:

Lastly, foo_test.py must contain an import statement like:

from src import Foo

因为我们从顶级目录执行,并且我们为每个子目录设置了包,所以 src 包在命名空间中可用,并且可以通过测试加载.

Because we are executing from the top level directory, AND we have packages setup for each of the subdirectories, the src package is available in the namespace, and can be loaded by a test.

这篇关于单元测试的 Python 相对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆