【问题标题】:Change names of tests created by nose test generators更改鼻子测试生成器创建的测试名称
【发布时间】:2012-06-26 17:31:13
【问题描述】:

Nose 有一个bug - 由生成器创建的测试名称没有被缓存,所以这个错误看起来像是发生在最后一个测试中,而不是它失败的实际测试。我在错误报告讨论中的solution 之后解决了它,但它仅适用于标准输出上显示的名称,而不适用于 XML 报告 (--with-xunit)

from functools import partial, update_wrapper
def testGenerator():
    for i in range(10):
        func = partial(test)
        # make decorator with_setup() work again
        update_wrapper(func, test)
        func.description = "nice test name %s" % i
        yield func

def test():
    pass

nose 的输出和预期的一样

nice test name 0 ... ok
nice test name 1 ... ok
nice test name 2 ... ok
...

但 XML 中的测试名称只是 'testGenerator'。

...<testcase classname="example" name="testGenerator" time="0.000" />...

如何更改此设置,以便个性化测试名称同时显示在标准输出和 XML 输出中?

我正在使用鼻子测试版本 1.1.2 和 Python 2.6.6

【问题讨论】:

  • 更改 'func.__name__' 或 'func.__doc__' 也不起作用。
  • 创建一个测试套件怎么样?
  • @Apalala 谢谢,这似乎可以解决它。虽然我找不到任何关于如何在鼻子中使用它以及它如何与鼻子的测试发现混合的信息。可以举个例子吗?
  • 上次我尝试过,nose 不会发现在运行时创建的测试。

标签: python unit-testing generator nose nosetests


【解决方案1】:

如果使用nose和Ecipe的PyUnit:

import nose

class Test(object):
    CURRENT_TEST_NAME = None

    def test_generator(self):
        def the_test(*args,**kwargs):
            pass

        for i in range(10):
            # Set test name
            Test.CURRENT_TEST_NAME = "TestGenerated_%i"%i
            the_test.description = Test.CURRENT_TEST_NAME

            # Yield generated test
            yield the_test,i

    # Set the name of each test generated
    test_generator.address = lambda arg=None:(__file__, Test, Test.CURRENT_TEST_NAME)

这将使名称在 PyUnit 中也能很好地显示出来。

【讨论】:

    【解决方案2】:

    正如 Ananth 所说,您可以使用它。

    testGenerator.__name__
    

    你也可以用这个代替

    testGenerator.compat_func_name
    

    如果您的测试类有参数,我建议对它们进行柯里化,以及使用 with_setup 进行柯里化。使用 lambda 可以节省导入,我认为它更干净一些。例如,

    from nose.tools import with_setup
    
    def testGenerator():
        for i in range(10):
            func = with_setup(set_up, tear_down)(lambda: test(i))
    
            func.description = "nice test name %s" % i
            testGenerator.compat_func_name = func.description
    
            yield func
    
    def test(i):
        pass
    
    def set_up():
        pass
    
    def tear_down():
        pass
    

    【讨论】:

    • 对咖喱的好呼吁
    【解决方案3】:

    您可以添加以下行。

    testGenerator.__name__ = "nice test name %s" % i

    例子:

    from functools import partial, update_wrapper
    def testGenerator():
        for i in range(10):
            func = partial(test)
            # make decorator with_setup() work again
            update_wrapper(func, test)
            func.description = "nice test name %s" % i
            testGenerator.__name__ = "nice test name %s" % i
            yield func
    
    def test():
        pass
    

    这将产生您想要的名称。

    <testsuite name="nosetests" tests="11" errors="0" failures="0" skip="0"><testcase classname="sample" name="nice test name 0" time="0.000" />
    

    【讨论】:

    • 这几乎可以工作,除了所有测试对我来说都是相同的名称(nose 1.2.1,python 2.7.3)
    【解决方案4】:

    您可以通过实现 describeTest 的 adding a plugin 更改 Nose 命名测试的方式

    from nose.plugins import Plugin
    class CustomName(Plugin):
        "Change the printed description/name of the test."
        def describeTest(self, test):
             return "%s:%s" % (test.test.__module__, test.test.description)
    

    然后您必须install this plugin,并在 Nose 调用中启用它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-31
      • 2014-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-02
      • 1970-01-01
      相关资源
      最近更新 更多