【问题标题】:Append the nose @attr to the test name将鼻子 @attr 附加到测试名称
【发布时间】:2014-11-10 06:08:30
【问题描述】:

我能够设置鼻子测试以使用@attr 标记运行。我现在很想知道我是否可以附加到测试名称的末尾,@attr 标记?我们要做的是添加一个标签,如果我们的测试遇到问题并且我们为它写了一个缺陷,然后我们会将缺陷编号作为@attr 标签。然后,当我们运行时,我们可以轻松识别哪些测试存在针对它们的公开缺陷。

只是想知道这是否可能,以及去哪里查看如何设置?

编辑结果运行结果:

测试结果:

所以我有点知道发生了什么,如果我在班级级别有@fancyattr(),它会捡起它并更改班级的名称。当我将@fancyattr() 放在测试级别时,它不会更改测试的名称,这是我需要做的。

例如 - 更改类的名称:

@dms_attr('DMSTEST')
@attr('smoke_login', 'smoketest', priority=1)
class TestLogins(BaseSmoke):

"""
Just logs into the system and then logs off
"""

def setUp(self):
    BaseSmoke.setUp(self)

def test_login(self):
    print u"I can login -- taking a nap now"
    sleep(5)
    print u"Getting off now"

def tearDown(self):
    BaseSmoke.tearDown(self)

这是我需要的,但它不起作用:

@attr('smoke_login', 'smoketest', priority=1)
class TestLogins(BaseSmoke):

    """
    Just logs into the system and then logs off
    """

    def setUp(self):
        BaseSmoke.setUp(self)

    @dms_attr('DMSTEST')  
    def test_login(self):
        print u"I can login -- taking a nap now"
        sleep(5)
        print u"Getting off now"

    def tearDown(self):
        BaseSmoke.tearDown(self)

更新了我在__doc__看到的截图:

【问题讨论】:

  • 更具体地说,您要修改带有属性的测试名称吗? IE。 @attr('slow')func_test 变成func_test_slow?
  • @Oleksiy 是的,就是这样。

标签: python unit-testing python-2.7 nosetests


【解决方案1】:

下面是如何使用 args 类型属性:

重命名测试.py:

import unittest 
from nose.tools import set_trace

def fancy_attr(*args, **kwargs):
    """Decorator that adds attributes to classes or functions
    for use with the Attribute (-a) plugin. It also renames functions!
    """
    def wrap_ob(ob):
        for name in args:
            setattr(ob, name, True)
            #using __doc__ instead of __name__ works for class methods tests
            ob.__doc__ = '_'.join([ob.__name__, name])
            #ob.__name__ = '_'.join([ob.__name__, name])

        return ob

    return wrap_ob


class TestLogins(unittest.TestCase):
    @fancy_attr('slow')
    def test_method():
        assert True


@fancy_attr('slow')
def test_func():
    assert True

运行测试:

$ nosetests rename_test.py -v
test_method_slow ... ok
test_func_slow ... ok

----------------------------------------------------------------------
Ran 2 tests in 0.003s

OK

编辑:要使 xunit 报告正常工作,应在运行测试之前进行测试重命名。您可以在导入时执行此操作,这里是未经测试的 hack,展示了如何执行此操作:

重命名测试.py:

import unittest 

def fancy_attr(*args, **kwargs):
    """Decorator that adds attributes to classes or functions
    for use with the Attribute (-a) plugin. It also renames functions!
    """
    def wrap_ob(ob):
        for name in args:
            setattr(ob, name, True)
            ob.__doc__ = '_'.join([ob.__name__, name])

        return ob

    return wrap_ob


class TestLogins(unittest.TestCase):
    @fancy_attr('slow')
    def test_method(self):
        assert True


def make_name(orig, attrib):
    return '_'.join([orig, attrib])

def rename(cls):
    methods = []
    for key in cls.__dict__:
        method = getattr(cls, key)
        if method:
            if hasattr(cls.__dict__[key], '__dict__'):
                if 'slow' in cls.__dict__[key].__dict__:
                    methods.append(key)

    print methods

    for method in methods:
        setattr(cls, make_name(method, 'slow'),  cls.__dict__[key])
        delattr(cls, method)


rename(TestLogins)

@fancy_attr('slow')
def test_func():
    assert True

【讨论】:

  • 我稍微修改了这一行:ob.__name__ = '_'.join([ob.__name__, name])ob.__name__ = str('_'.join([ob.__name__, name])),因为当我运行它时,它给了我一个类型错误,说 __name__ 期待一个字符串对象。
  • 我在测试结果中没有看到重命名。
  • 如果你能提供一个具体的例子会有所帮助。
  • 我是否需要在我的测试中为 Nose 提供一个导入语句,例如 import Plugin 之类的东西才能正常工作?
  • 不,它应该按原样工作,这是一个完整的示例。您必须通过 -a 才能根据属性选择测试,但实现方式使运行测试执行重命名,而不是插件激活。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-06-27
  • 2012-06-26
  • 1970-01-01
  • 1970-01-01
  • 2012-10-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多