我可以设置鼻子测试来运行@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__
:
答案 0 :(得分:1)
以下是使用args类型属性的方法:
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报告正常工作,应在运行测试之前进行测试重命名。您可以在导入时执行此操作,这是未经测试的黑客,显示如何执行此操作:
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