如何防止nose导入__init__.py文件?
能否指示 nose 测试框架仅在 test_*.py< 中运行测试/代码> 文件?
事实上,使用以下目录结构进行 nosetests A
:
A/
test_A.py
B/
__init__.py
导入 B,这是我想避免的。
原因是 B 模块以 import numpy 开头,因为它仅在用户安装了可选的 NumPy 模块时才使用。但是,未安装 NumPy 的用户不希望鼻子测试处理 B/__init__.py
,因为即使 NumPy 是可选的,它在 import numpy
上也必然会失败。如何才能实现这一目标?
Can the nose testing framework be instructed to only run tests in test_*.py
files?
In fact, doing nosetests A
with the following directory structure:
A/
test_A.py
B/
__init__.py
imports B, which I want to avoid.
The reason for this is that the B module starts with import numpy
because it is only meant to be used when the user has the optional NumPy module installed. However, users who did not install NumPy do not want nosetests to process B/__init__.py
, because it necessarily fails on import numpy
even though NumPy is optional. How can this be achieved?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
只需使用
try:.. except ImportError:...
块包装导入即可。在这种情况下,您甚至可以设置一个变量,让您知道 numpy 是否可用。Simply wrap the import with a
try:..except ImportError:...
block. In that case you could even set a variable letting you know if numpy is available or not.当然,只需使用--match和--exclude命令行选项来限制nose将发现的测试程序。
Sure, just use the --match and the --exclude command line options to limit what nose will discover as a test program.
Nose 可以接受/拒绝目录、文件、模块、类和方法级别的测试。您需要拒绝
B
目录。没有办法忽略__init__.py
文件; Python 从来没有见过它。它在导入B
时出现,因此您需要忽略B
。尝试:
Nose can accept/reject tests at the directory, file, module, class and method levels. You need to reject the
B
directory. There is no way to ignore the__init__.py
file; Python never sees it. It shows up whenB
is imported, so you need to ignoreB
.Try:
我认为鼻子排除插件可能会有所帮助。如果您安装此插件然后运行:
它可能适合您。我也有同样的问题,并取得了一些还算可以的结果。现在我的下一个问题是,在创建 notests 配置文件时,
exclude-dir
似乎不是一个可用的选项。I think the nose-exclude plugin may help. If you install this plugin and then run:
It may work for you. I have the same problem and have achieved some passable results. Now my next issue is that the
exclude-dir
doesn't seem to be a usable option when creating a nosetests config file.