python nose测试框架全面介绍十---用例的跳过

又来写nose了,这次主要介绍nose中的用例跳过应用,之前也有介绍,见python nose测试框架全面介绍四,但介绍的不详细。下面详细解析下

nose自带的SkipTest

先看看nose自带的SkipTest典型应用

 应用一:

‘''
@auth:hu
’''
from nose.plugins.skip import SkipTest
@attr(mode=1)
def test_learn_1():
raise SkipTest

但这种SkipTest在实际的日志中没有显示Skip关键字

应用二:

如果想要在执行用例前判断相关字雄姿英发再进行用例跳过,如下

'''

@author: huzq
'''
import nose
import nose.plugins.multiprocess
from testtools import TestCase
from nose import SkipTest
from nose.plugins.skip import Skip
import unittest class TestClass(): def setUp(self):
print "MyTestClass setup"
if "xxx" in "qqqq":
raise SkipTest("adfdfd") def Testfunc1(self):
print "this is Testfunc01"

每个用例前都会进行判断,并进行跳过操作

应用三:

可用觉得写在代码中太麻烦,SkipTest也可以当装饰器来进行,如下

'''
@author: huzq
'''
import nose
import nose.plugins.multiprocess
from testtools import TestCase
from nose import SkipTest
from nose.plugins.skip import Skip
import unittest class TestClass(): @classmethod
def setUpClass(self):
print "xxxxxx" def setUp(self):
print "MyTestClass setup" def tearDown(self):
print "MyTestClass teardown" @SkipTest
def Testfunc1(self):
print "this is Testfunc01"

要注意的时,如果用装饰器形式,SkipTest后不能接具体原因,如果要接具体原因,只能用unittest的方法写,如下

@unittest.skip("I don't want to run this case.")

SkipTest可放在SetUpclass、SetUp、Test中

使用unittest的skip


什么都是没有完美的,如果想用装饰器来进行用例判断并跳过,nose自带的skiptest没法完成。好在unittest有更好的解决方案。

就直接贴官网的例子吧,nose也支持

class MyTestCase(unittest.TestCase):

    @unittest.skip("demonstrating skipping")
def test_nothing(self):
self.fail("shouldn't happen") @unittest.skipIf(mylib.__version__ < (1, 3),
"not supported in this library version")
def test_format(self):
# Tests that work for only a certain version of the library.
pass @unittest.skipUnless(sys.platform.startswith("win"), "requires Windows")
def test_windows_support(self):
# windows specific testing code
pass
上一篇:mysql中事务的并发问题与隔离级别


下一篇:python nose测试框架全面介绍七--日志相关