将nose@attr附加到测试名称

2024-10-01 11:32:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我可以设置鼻子测试运行@attr标签。我现在想知道是否可以在测试名称的末尾添加@attr标记?我们要做的是添加一个标记如果我们的测试遇到问题,我们为它写了一个缺陷,然后我们将把缺陷编号作为@attr标记。然后,当我们运行时,我们就可以很容易地识别出哪些测试对它们有开放的缺陷。在

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

编辑结果运行时回答:enter image description hereenter image description here

试验结果: enter image description here

所以我有点知道发生了什么,如果在类级别有@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)

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

^{pr2}$

更新了我看到的__doc__屏幕截图:

enter image description here


Tags: 标记self名称defsetuplogin级别now
1条回答
网友
1楼 · 发布于 2024-10-01 11:32:00

以下是如何使用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

运行试验:

^{pr2}$

编辑:为了让xunit报告正常工作,应该在运行测试之前对测试进行重命名。您可以在import上完成,下面是未经测试的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

相关问题 更多 >