[英]Python repository organization w.r.t. tests and PyDev
I'm trying to set up a Python repository for some code. 我正在尝试为一些代码建立一个Python存储库。 I've read the Structuring Your Project tutorial and set everything up as suggested.
我已经阅读了《 构建项目》教程,并按照建议进行了所有设置。 Concretely, I have the following directory structure:
具体而言,我具有以下目录结构:
repo_root/
some_module.py
tests/
context.py
test_some_module.py
The context.py
file contains exactly what the tutorial suggests: context.py
文件完全包含本教程建议的内容:
import os
import sys
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
import some_module
The test_some_module.py
file imports some_module
like so: test_some_module.py
文件导入some_module
如下所示:
from context import some_module
instead of 代替
from .context import some_module
as the site suggests. 如网站所建议。
I'm using PyDev and running the unit tests by right-clicking on the test file and selecting Run As/unittest
. 我正在使用PyDev并通过右键单击测试文件并选择
Run As/unittest
来Run As/unittest
单元测试。 This causes the test to run in the tests
directory, where context
is visible and the import is successful. 这将导致测试在
tests
目录中运行,该目录可见context
并且导入成功。 PyDev shows Unresolved import
. PyDev显示
Unresolved import
。 I'm guessing this is because it runs the parser from the project root. 我猜这是因为它从项目根目录运行解析器。 If I add
tests
to the PYTHONPATH
it still doesn't work. 如果我将
tests
添加到PYTHONPATH
它仍然无法正常工作。 Only if I add an __init__.py
file in tests
does PyDev stop showing errors, because it's treating tests
as a library. 只有在
tests
中添加__init__.py
文件时,PyDev才会停止显示错误,因为它将tests
视为库。
What is the correct way to set up my repository? 设置存储库的正确方法是什么? Are tests supposed to only be called in the
tests
directory or should they also be start-able from the repository root? 是应该仅在
tests
目录中调用tests
还是应该从存储库根目录启动这些tests
?
如果您的包中没有__init__
文件(即使是用于测试),PyDev的效果也不佳,因此,我的建议是在其中添加__init__
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.