python – 将鼻子@attr添加到测试名称

python – 将鼻子@attr添加到测试名称,第1张

概述我可以设置鼻子测试来运行@attr标签.我现在有兴趣知道我是否可以附加到测试名称末尾的@attr标签?我们要做的是添加一个标签,如果我们的测试遇到问题并且我们为它编写了一个缺陷,那么我们会将缺陷编号作为@attr标记.然后,当我们运行时,我们可以轻松识别哪些测试具有针对它们的开放缺 只是想知道这是否可行,以及在哪里查看如何设置它? 编辑结果与答案一起运行: 检测结果: 所以我知道发生了什么,如果我 我可以设置鼻子测试来运行@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',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__更新了我所看到的截图:

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

rename_test.py:

import unittest from nose.tools import set_tracedef 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_obclass TestLogins(unittest.TestCase):    @fancy_attr('slow')    def test_method():        assert True@fancy_attr('slow')def test_func():    assert True

运行测试:

$nosetests rename_test.py -vtest_method_slow ... oktest_func_slow ... ok----------------------------------------------------------------------Ran 2 tests in 0.003sOK

编辑:要使xunit报告正常工作,应在运行测试之前进行测试重命名.您可以在导入时执行此 *** 作,这是未经测试的黑客,显示如何执行此 *** 作:

rename_test.py:

import unittest def fancy_attr(*args,True)            ob.__doc__ = '_'.join([ob.__name__,name])        return ob    return wrap_obclass TestLogins(unittest.TestCase):    @fancy_attr('slow')    def test_method(self):        assert Truedef 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
总结

以上是内存溢出为你收集整理的python – 将鼻子@attr添加到测试名称全部内容,希望文章能够帮你解决python – 将鼻子@attr添加到测试名称所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1197152.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-03
下一篇2022-06-03

发表评论

登录后才能评论

评论列表(0条)

    保存