将鼻子@attr附加到测试名称 [英] Append the nose @attr to the test name

查看:96
本文介绍了将鼻子@attr附加到测试名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够设置鼻子测试以与@attr标记一起运行.我现在有兴趣知道是否可以在测试名称的末尾附加@attr标签吗?我们试图做的是,如果我们的测试遇到问题并为其添加缺陷,则添加一个标签,然后将缺陷编号作为@attr标签.然后,当我们运行时,我们可以轻松地确定哪些测试存在针对它们的开放性缺陷.

I am able to setup nose tests to run with the @attr tag. I am now interested in know if I can append to the end of the test name, the @attr tag? What we are trying to do is add a tag if our tests run into an issue and we write up a defect for it, we would then put the defect number as an @attr tag. Then when we run we could easily identify which tests have open defects against them.

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

Just wondering if this is even possible, and where to go to see how to set it up?

使用答案运行的编辑结果:

EDIT RESULTS RUNNING WITH ANSWER:

测试结果:

所以我有点知道发生了什么,如果我在类级别具有@fancyattr(),它将选择它并更改类的名称.当我将@fancyattr()放在测试级别时,它并没有更改测试的名称,这是我需要执行的操作.

So I sort of know what is going on, if I have the @fancyattr() at the class level it picks it up and changes the name of the class. When I put the @fancyattr() at the test level it is not changing the name of the test, which is what I need for it to do.

例如-更改类的名称:

@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)

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

This is what I need and it isn't working:

@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__中看到的内容更新了屏幕截图:

Updated screenshot with what I am seeing with __doc__:

推荐答案

以下是使用args类型属性的方法:

Here is how to do it with args type attributes:

rename_test.py:

rename_test.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,显示了如何执行此操作:

For xunit reporting to work, test renaming should take place before running the test. You can do it on import, here is untested hack showing how to do it:

rename_test.py:

rename_test.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

这篇关于将鼻子@attr附加到测试名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆