运行生成的鼻子测试

发布于 2024-10-31 08:08:51 字数 609 浏览 4 评论 0原文

假设我定义了一个 testFile.py python 模块,如下所示。

def test_evens():
    for i in range(0, 5):
        yield check_even, i, i*3

def check_even(n, nn):
    assert n % 2 == 0 or nn % 2 == 0

当我让鼻子识别仅收集模式下的测试时,我发现

testFile.test_evens(0, 0) ... ok
testFile.test_evens(1, 3) ... ok
testFile.test_evens(2, 6) ... ok
testFile.test_evens(3, 9) ... ok
testFile.test_evens(4, 12) ... ok

我可以使用以下命令运行所有测试

nosetests -v testFile:test_evens

但是,如果我只想运行 testFile.test_evens(2, 6) (即不是所有测试)怎么办?

有什么方法可以从命令行执行此操作吗?

Suppose I define a testFile.py python module with as follows.

def test_evens():
    for i in range(0, 5):
        yield check_even, i, i*3

def check_even(n, nn):
    assert n % 2 == 0 or nn % 2 == 0

When I let the nose identify the tests in collect-only mode I get

testFile.test_evens(0, 0) ... ok
testFile.test_evens(1, 3) ... ok
testFile.test_evens(2, 6) ... ok
testFile.test_evens(3, 9) ... ok
testFile.test_evens(4, 12) ... ok

I can run all tests using

nosetests -v testFile:test_evens

However, what if I only want to run testFile.test_evens(2, 6) (i.e., not all the tests)?

Is there any way to do this from the command line?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏见 2024-11-07 08:08:51

据我所知,鼻子默认无法做到这一点。以下是一些选项:

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

Nose cannot do this by default, to my knowledge. Here are some options:

1. Fake it from the command line

Probably not what you're looking for, but I had to mention it. You could also
create a wrapper script to simplify this:

python -c 'import testFile; testFile.check_even(2, 6)'

2. Create a custom nose test loader

This is a little more involved, but you can create a custom test
loader which interprets the command-line arguments as specifying the generator
to load, pulls out tests and arguments from the generator, and returns a suite containing the test(s) with the matching arguments.

Below is some example code which should give you enough to build on (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)

Executing it:

% 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文