带有命令行参数的鼻子测试脚本
我希望能够运行接受命令行参数的鼻子测试脚本。例如,类似这样的内容:
test.py
import nose, sys
def test():
# do something with the command line arguments
print sys.argv
if __name__ == '__main__':
nose.runmodule()
但是,每当我使用命令行参数运行此命令时,都会收到错误:
$ python test.py arg
E
======================================================================
ERROR: Failure: ImportError (No module named arg)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/nose-0.11.1-py2.6.egg/nose/loader.py", line 368, in loadTestsFromName
module = resolve_name(addr.module)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/nose-0.11.1-py2.6.egg/nose/util.py", line 334, in resolve_name
module = __import__('.'.join(parts_copy))
ImportError: No module named arg
----------------------------------------------------------------------
Ran 1 test in 0.001s
FAILED (errors=1)
显然,nose 尝试对 sys.argv 中传递的参数执行某些操作。有没有办法让鼻子忽略这些论点?
I would like to be able to run a nose test script which accepts command line arguments. For example, something along the lines:
test.py
import nose, sys
def test():
# do something with the command line arguments
print sys.argv
if __name__ == '__main__':
nose.runmodule()
However, whenever I run this with a command line argument, I get an error:
$ python test.py arg
E
======================================================================
ERROR: Failure: ImportError (No module named arg)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/nose-0.11.1-py2.6.egg/nose/loader.py", line 368, in loadTestsFromName
module = resolve_name(addr.module)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/nose-0.11.1-py2.6.egg/nose/util.py", line 334, in resolve_name
module = __import__('.'.join(parts_copy))
ImportError: No module named arg
----------------------------------------------------------------------
Ran 1 test in 0.001s
FAILED (errors=1)
Apparently, nose tries to do something with the arguments passed in sys.argv. Is there a way to make nose ignore those arguments?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
好吧,我讨厌“你为什么要这么做?”答案和其他人一样多,但我必须在这里做出一个。我希望你不介意。
我认为做任何你想做的事情都不在框架鼻子的范围内。 Nose 旨在用于自动化测试。如果您必须传递命令行参数才能通过测试,那么它就不是自动化的。现在,您可以做的是这样的:
如果您运行它,您会得到以下输出:(
如果您想查看标准输出上发生的情况,请记住传递 -s 标志)
但是,我可能仍然建议反对这样做,因为如果可以避免的话,在自动化测试中弄乱全局状态通常是一个坏主意。我可能会做的是调整我想要测试的任何代码以获取 argv 列表。然后,您可以在测试期间传入您想要的任何内容,并在生产中传入 sys.argv 。
更新:
听起来您可能想尝试编写一个鼻子插件。这很容易做到。 这里是最新的文档。
Alright, I hate "why would you want to do that?" answers just as much as anyone, but I'm going to have to make one here. I hope you don't mind.
I'd argue that doing whatever you're wanting to do isn't within the scope of the framework nose. Nose is intended for automated tests. If you have to pass in command-line arguments for the test to pass, then it isn't automated. Now, what you can do is something like this:
If you run that, you get this output:
(Remember to pass in the -s flag if you want to see what goes on stdout)
However, I'd probably still recommend against that, as it's generally a bad idea to mess with global state in automated tests if you can avoid it. What I would likely do is adapt whatever code I'm wanting to test to take an
argv
list. Then, you can pass in whatever you want during testing and pass insys.argv
in production.UPDATE:
It sounds like you may want to try your hand at writing a nose plugin. It's pretty easy to do. Here are the latest docs.
您可以使用另一种方法将内容添加到代码中:
然后记住在运行鼻子之前设置环境。
You could use another means of getting stuff into your code:
Then just remember to set your environment before running nose.
我认为这是一个完全可以接受的场景。我还需要做类似的事情,以便针对不同的场景(开发、质量保证、产品等)运行测试,并且我需要为每个环境提供正确的 URL 和配置。
我找到的解决方案是使用 nose-testconfig 插件(链接在这里< /a>)。它并不完全传递命令行参数,而是使用所有参数创建一个配置文件,然后在执行鼻子测试时将此配置文件作为参数传递。
配置文件具有以下格式:
您可以使用以下方式读取参数:
I think that is a perfectly acceptable scenario. I also needed to do something similar in order to run the tests against different scenarios (dev, qa, prod, etc) and there I needed the right URLS and configurations for each environment.
The solution I found was to use the nose-testconfig plugin (link here). It is not exactly passing command line arguments, but creating a config file with all your parameters, and then passing this config file as argument when you execute your nose-tests.
The config file has the following format:
And you can read the arguments using:
现在我正在使用以下 hack:
它只是将参数读取到局部变量中,然后删除 sys.argv 中的所有附加参数,以便鼻子不会被它们混淆。
For now I am using the following hack:
which just reads the argument into a local variable, and then deletes all the additional arguments in
sys.argv
so that nose does not get confused by them.仅运行鼻子并传入参数是行不通的,因为鼻子会尝试将参数解释为鼻子参数,因此您会遇到您所看到的问题。
我不认为nose支持直接参数传递,但是这个nose插件 nose-testconfig 允许您编写如下测试:
Just running nose and passing in parameters will not work as nose will attempt to interpret the arguments as nose parameters so you get the problems you are seeing.
I do not think nose support parameter passing directly yet but this nose plug-in nose-testconfig Allows you to write tests like below: