简体   繁体   English

Python 导入以使用鼻子进行测试 - 导入高于当前 package 的模块的最佳做法是什么

[英]Python imports for tests using nose - what is best practice for imports of modules above current package

This is a question which is asked frequently in different forms, and often obtains "lol you're not doing it properly" responses.这是一个在不同的 forms 中经常被问到的问题,并且经常得到“大声笑你做得不好”的回答。 Pretty sure that's because there's a common sense scenario people (including me) are trying to use as an implementation, and the solution is not obvious (if you've not done it before).很确定这是因为人们(包括我)正在尝试将其用作实现的常识场景,并且解决方案并不明显(如果您以前没有这样做过)。

Would accept an answer which "lets the fly out of the bottle".会接受“让飞出瓶子”的答案。

Given给定

project/
    __init__.py
    /code
        __init__.py
        sut.py
    /tests
        __init__.py
        test_sut.py

Where tests_sut.py starts: tests_sut.py 开始的地方:

import code.sut

Running nosetests in the root dir leads to:在根目录中运行鼻子测试会导致:

ImportError: No module named code.sut

Avenues traveled:走过的大道:

a) do a relative using a)做一个亲戚使用

from ..code import sut

b) add root of project to PYTHONPATH b) 将项目的根目录添加到 PYTHONPATH

c) use the c) 使用

sys.path.append

to add the.. path before the imports at the start of each test module.在每个测试模块开始处的导入之前添加.. 路径。

d) just remember to do a d) 只记得做一个

setup.py 

on the project to install the modules into the site-packages before running tests.在项目上将模块安装到站点包中,然后再运行测试。


So the requirement is to have tests located beneath the test package root which have access to the project.因此,要求是在测试 package 根目录下进行测试,这些测试可以访问项目。 Each of the above don't feel "natural" to me, have proved problematic or seem like too much hard work!以上每一项对我来说都不“自然”,被证明是有问题的,或者看起来太辛苦了!

In java this works, but basically by dint of your build tool / IDE placing all your classes on the classpath.在 java 中这是可行的,但基本上是通过您的构建工具 / IDE 将所有类放在类路径上。 Perhaps the issue is I'm expecting "magic" from Python?也许问题是我期待 Python 的“魔法”? Have noted in the Flask webframework tests, option d) seems to be preferred.在 Flask 网络框架测试中已经注意到,选项 d) 似乎是首选。

In any case, statements below recommending a preferred solution would remove the feeling of "unnaturalness" in my own.无论如何,以下推荐首选解决方案的陈述将消除我自己的“不自然”感觉。

I had the same problem and found an answer in a related question work for me.我遇到了同样的问题,并在我的相关问题工作中找到了答案

Just remove the __init__.py in the project root.只需删除项目根目录中的 __init__.py 即可。

You have answered your question pretty well already.. D (install to system location) is preferred for distributable code.您已经很好地回答了您的问题。 D(安装到系统位置)是可分发代码的首选。 I usually use C (modify sys.path) because I don't want system-wide installs of my hundreds of custom libs.我通常使用 C(修改 sys.path),因为我不想在系统范围内安装数百个自定义库。 In theory A (relative import) seems nicer, but there are cases where it fails.理论上 A(相对导入)似乎更好,但在某些情况下它会失败。 B (PYTHONPATH) is right out, really only for testing purposes in my opinion. B (PYTHONPATH) 是正确的,在我看来真的只是为了测试目的。

That pretty much sums up all of the options.这几乎总结了所有选项。 The option you prefer (Python magically knows where to look) is really not a workable solution because it can lead to unpredictable results, such as automagically finding libraries from unrelated projects.您喜欢的选项(Python 神奇地知道在哪里寻找)实际上不是一个可行的解决方案,因为它可能导致不可预测的结果,例如从不相关的项目中自动查找库。

In my opinion, the best thing to do is put this at the entry point(s) to your program:在我看来,最好的办法是把它放在你程序的入口点:

import sys, os
sys.path = [os.path.abspath(os.path.dirname(__file__))] + sys.path

I know there is a answer checked and I still think it's a good reason to share other alternatives:)我知道已经检查了一个答案,我仍然认为这是分享其他替代方案的好理由:)

There is a nose-pathmunge giving you a control to set sys.path while invoking nosestests .在调用nosestests时,有一个nose-pathmunge可以让您控制设置sys.path

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM