如何对目录中的所有测试运行扭曲试验?
如何运行试用以便它执行目录中的所有测试?如果我单独对每个文件运行试用,那么我的所有单元测试都会通过,但是如果我在测试目录上尝试类似...
trial test/
的操作,它会给我一个“PASSED
”,以及以下消息...
UserWarning: (for module __init__) not in path importer cache (PEP 302 violation
- check your local configuration).
而不是实际运行目录中的所有测试。
How do I run trial so that it executes all tests within a directory? All my unit tests pass if I run trial on each file individually, but if I try something like...
trial test/
on the test directory, it gives me one "PASSED
", and the following message...
UserWarning: (for module __init__) not in path importer cache (PEP 302 violation
- check your local configuration).
rather than actually running all the tests within the directory.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先:你不能调用你的顶级单元测试包
test
。这是 Python 单元测试的名称,因此您将永远无法在已安装的配置中运行测试,并且根据 python 的设置方式,您最终可能会导入 python 自己的测试而不是您自己的测试。第二:sys.path 是一个巨大而微妙的谜团。
Trial
支持在文件和目录上运行作为快速入门技巧,但它永远不可能真正完全正确地使用路径名。正确的做法是传递一个模块(或包)名称,它可以作为 python 模块导入并检查。因此,如果您的目录结构如下所示:
那么您应该像这样运行测试:
换句话说,不要从项目目录中运行测试;这会在源代码中转储 _torial_temp 目录,混淆“我加载代码的位置”和“当前目录”,并且通常会造成各种事情的混乱,以后很难理清。
因此,使用您选择的路径管理工具设置您的
PYTHONPATH
和PATH
:组合器,setup.py开发,virtualenv – 或者只是将垃圾转储到您的~/.bashrc中
– 然后从某个临时位置在一个唯一命名的顶级 Python 包上运行Trial
,一切都会正常工作。First of all: you can't call your top-level unit test package
test
. That's the name of Python's unit tests, so you will never be able to run your tests in an installed configuration, and depending on how your python is set up, you may end up importing python's own tests instead of your own.Second:
sys.path
is a vast and subtle mystery.trial
supports running on files and directories as a quick getting-started hack, but it can never really be completely correct about using path names. The right thing to do is to pass trial a module (or package) name, that it can import as a python module and inspect.So if your directory structure looks like:
then you should run your tests like this:
in other words, don't run your tests from within your project's directory; this dumps _trial_temp directories all over your source code, confuses "the place I load my code from" and "the current directory" and generally makes a muddle of various things which can be difficult to untangle later.
So, set up your
PYTHONPATH
andPATH
using the path-management tool of your choice: Combinator, setup.py develop, virtualenv – or just dumping junk into your~/.bashrc
– and then runtrial
from some temporary location, on a uniquely-named top-level Python package, and everything should work just fine.