python 带有命令行参数的鼻子测试脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1660520/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 22:49:10  来源:igfitidea点击:

Nose test script with command line arguments

pythoncommand-line-argumentsnose

提问by dzhelil

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?

显然,nose 试图用 sys.argv 中传递的参数做一些事情。有没有办法让鼻子忽略这些论点?

回答by Jason Baker

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 automatedtests. If you have to pass in command-line arguments for the test to pass, then it isn't automated. Now, what you cando is something like this:

我认为做任何你想做的事情都不在框架鼻子的范围内。Nose 用于自动化测试。如果您必须传入命令行参数才能通过测试,那么它就不是自动化的。现在,你可以做的是这样的:

import sys

class test_something(object):
    def setUp(self):
        sys.argv[1] = 'arg'
        del sys.argv[2] # remember that -s is in sys.argv[2], see below
    def test_method(self):
        print sys.argv

If you run that, you get this output:

如果你运行它,你会得到这个输出:

[~] nosetests test_something.py -s
['/usr/local/bin/nosetests', 'arg']
.
----------------------------------------------------------------------
Ran 1 test in 0.001s

OK

(Remember to pass in the -s flag if you want to see what goes on stdout)

(如果您想查看 stdout 上的内容,请记住传入 -s 标志)

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 argvlist. Then, you can pass in whatever you want during testing and pass in sys.argvin production.

但是,我可能仍然建议不要这样做,因为如果可以避免的话,在自动化测试中弄乱全局状态通常是一个坏主意。我可能会做的是调整我想要测试的任何代码以获取argv列表。然后,您可以在测试期间传入任何您想要的内容并sys.argv在生产中传入。

UPDATE:

更新

The reason why I need to do it is because I am testing multiple implementations of the same library. To test those implementations are correct I use a single nose script, that accepts as a command line argument the library that it should import for testing.

我需要这样做的原因是因为我正在测试同一个库的多个实现。为了测试这些实现是否正确,我使用了一个单一的鼻子脚本,它接受它应该导入进行测试的库作为命令行参数。

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.

听起来您可能想尝试编写鼻子插件。这很容易做到。 这里是最新的文档。

回答by Bemis

You could use another means of getting stuff into your code:

您可以使用另一种方法将内容放入代码中:

import os

print os.getenv('KEY_THAT_MIGHT_EXIST', default_value)

Then just remember to set your environment before running nose.

然后只记得在流鼻涕之前设置你的环境。

回答by Thiago Burgos

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.

我认为这是一个完全可以接受的场景。我还需要做一些类似的事情,以便针对不同的场景(开发、质量验证、生产等)运行测试,并且我需要为每个环境提供正确的 URLS 和配置。

The solution I found was to use the nose-testconfigplugin (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.

我找到的解决方案是使用nose-testconfig插件(链接在这里)。它并不是完全传递命令行参数,而是创建一个包含所有参数的配置文件,然后在执行鼻子测试时将此配置文件作为参数传递。

The config file has the following format:

配置文件具有以下格式:

[group1]
env=qa

[urlConfig]
address=http://something

[dbConfig]
user=test
pass=test

And you can read the arguments using:

您可以使用以下方法读取参数:

from testconfig import config

print(config['dbConfig']['user'])

回答by dzhelil

For now I am using the following hack:

现在我正在使用以下技巧:

args = sys.argv[1:]
sys.argv = sys.argv[0:1]

which just reads the argument into a local variable, and then deletes all the additional arguments in sys.argvso that nose does not get confused by them.

它只是将参数读入局部变量,然后删除所有附加参数,sys.argv以便鼻子不会被它们混淆。

回答by Tendayi Mawushe

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-testconfigAllows you to write tests like below:

我不认为鼻子支持直接传递参数但是这个鼻子插件nose-testconfig允许您编写如下测试:

from testconfig import config
def test_os_specific_code():
    os_name = config['os']['type']
    if os_name == 'nt':
        pass # some nt specific tests
    else:
        pass # tests for any other os