您的位置:首页 > 编程语言 > Python开发

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

2017-11-18 10:56 441 查看
之前写了一系列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"


转载请标明来源:landhu.cnblogs.com
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: