用鼻子重复一次或多次测试 [英] Repeated single or multiple tests with Nose
问题描述
类似于此问题,我想让鼻子进行n
次测试(或所有测试),但不能同时进行 .
Similar to this question, I'd like to have Nose run a test (or all tests) n
times -- but not in parallel.
我在一个项目中有几百个测试;一些是一些简单的单元测试.其他是带有某种程度的并发性的集成测试.经常在调试测试时,我想更击中"测试; bash循环可以工作,但是会产生很多混乱的输出-不再有单个的".每次通过测试.能够胜任某些测试的选定测试的能力似乎是要求Nose进行的自然选择,但我在文档中的任何地方都没有找到它.
I have a few hundred tests in a project; some are some simple unit tests. Others are integration tests w/ some degree of concurrency. Frequently when debugging tests I want to "hit" a test harder; a bash loop works, but makes for a lot of cluttered output -- no more nice single "." for each passing test. Having the ability to beat on the selected tests for some number of trials seems like a natural thing to ask Nose to do, but I haven't found it anywhere in the docs.
让Nose做到这一点的最简单方法是什么(bash循环除外)?
What's the simplest way to get Nose to do this (other than a bash loop)?
推荐答案
You can write a nose test as a generator, and nose will then run each function yielded:
def check_something(arg):
# some test ...
def test_something():
for arg in some_sequence:
yield (check_something, arg)
使用 nose-testconfig ,您可以使测试次数成为命令行参数:
Using nose-testconfig, you could make the number of test runs a command line argument:
from testconfig import config
# ...
def test_something():
for n in range(int(config.get("runs", 1))):
yield (check_something, arg)
您希望在命令行中使用例如
Which you'd call from the command line with e.g.
$ nosetests --tc=runs:5
...多次运行.
或者(也使用鼻子测试配置),您可以编写一个装饰器:
Alternatively (but also using nose-testconfig), you could write a decorator:
from functools import wraps
from testconfig import config
def multi(fn):
@wraps(fn)
def wrapper():
for n in range(int(config.get("runs", 1))):
fn()
return wrapper
@multi
def test_something():
# some test ...
然后,如果要将测试分为不同的组,每个组都有自己的命令行参数来表示运行次数:
And then, if you want to divide your tests into different groups, each with their own command line argument for the number of runs:
from functools import wraps
from testconfig import config
def multi(cmd_line_arg):
def wrap(fn):
@wraps(fn)
def wrapper():
for n in range(int(config.get(cmd_line_arg, 1))):
fn()
return wrapper
return wrap
@multi("foo")
def test_something():
# some test ...
@multi("bar")
def test_something_else():
# some test ...
您可以这样拨打电话:
$ nosetests --tc=foo:3 --tc=bar:7
这篇关于用鼻子重复一次或多次测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!