python nose测试框架全面介绍五--attr介绍

之前写了一系列nose框架的,这篇介绍下attr tag

在nose框架中attr用来标识用例,使得在运行时可以通过标识来执行用例,之前在nose测试框架全面介绍四中有说明,但没有说明清楚,这里再总结下。

一、标识方式

   标识方式有二种:

    1、方式一(不太好用)

       

def test_learn_1():
    u'''测试'''
    print 'xxx'
    eq_(7, 7, msg=u"错误")
    
test_learn_1.slow=1

 2、使用attr装饰器

from nose.plugins.attrib import attr
@attr(mode=2) 
def test_lean_2():
    u'''测试失败'''
    try:
        print "test_learn_2"
        ok_(4==3,msg="xxx")
        print sys._getframe().f_code.co_name
    except Exception:
        print sys._getframe().f_code.co_name

或直接attr

@attr('afdfd') 
def test_lean_3():
    u'''测试成功'''
    pass

二、运行

运行方式如下:

使用-a或者--attr来标识

nosetests -a mode=2

表示只运行标识中mode等于2的

nosetests -a mode=2,afdfd

表示运行同时包含标识中mode等于2及标识中有afdfd的用例,注意,是同时包含

nosetests -a mode=2 -a safdfd

表示运行包含标识mode等于2或者标识中safdfd的用例,注意,是或者的关系

同样,-a后面还可以接正则表达式来运行,不过,我目前用的很少,基本上没用到,也介绍下吧

注意,要使用这种方式的话,要用-A或--eval-attr参数来标识

nosetests -A "not afdfd" 
nosetests -A "(mode > 5) and not afdfd"

 注意:一定要用双引号,单引号会提示 系统找不到指定的文件

2018-9.3更新==================

attr还可以直接放在一个类前,如

@attr("bbb")
class test_bbb(TestCase):
    def test_bbb1(self):
        print "ffff"

  

然后-a 带bbb时,只会测试这个类

如果在类和含数中都有attr,只单独测一个怎么办呢?如:

@attr("bbb")
class test_bbb(TestCase):
    @attr("fff")
    def test_bbb1(self):
        print "ffff"

class test_aaaa(test_bbb):
    def test_qqqq(self):
        print "ffff"

-a bbbb,fff  就只会测test_bbb类中的test_bbb1函数

 转载请标明来源:landhu.cnblogs.com

原文地址:https://www.cnblogs.com/landhu/p/7856135.html