据我所知,鼻子默认无法做到这一点。以下是一些选项:
1.从命令行伪造它
可能不是你要找的,但我不得不提一下。您还可以创建一个包装脚本来简化这一点:
python -c 'import testFile; testFile.check_even(2, 6)'
2.创建自定义鼻子测试加载器
这有点复杂,但您可以创建一个自定义测试加载器,它将命令行参数解释为指定要加载的生成器,从生成器中提取测试和参数,并返回一个包含测试的套件匹配的参数。
下面是一些示例代码,应该足以让您在 ( runner.py ) 的基础上进行构建:
import ast
import nose
class CustomLoader(nose.loader.TestLoader):
def loadTestsFromName(self, name, module=None):
# parse the command line arg
parts = name.split('(', 1)
mod_name, func_name = parts[0].split('.')
args = ast.literal_eval('(' + parts[1])
# resolve the module and function - you'll probably want to
# replace this with nose's internal discovery methods.
mod = __import__(mod_name)
func = getattr(mod, func_name)
# call the generator and gather all matching tests
tests = []
if nose.util.isgenerator(func):
for test in func():
_func, _args = self.parseGeneratedTest(test)
if _args == args:
tests.append(nose.case.FunctionTestCase(_func, arg=_args))
return self.suiteClass(tests)
nose.main(testLoader=CustomLoader)
执行它:
% python runner.py 'testFile.test_evens(2, 6)' -v
testFile.check_even(2, 6) ... ok
% python runner.py 'testFile.test_evens(2, 6)' 'testFile.test_evens(4, 12)' -v
testFile.check_even(2, 6) ... ok
testFile.check_even(4, 12) ... ok
% python runner.py 'testFile.test_evens(1, 3)' -v
testFile.check_even(1, 3) ... FAIL