python-unittest跳过测试方法

@unittest.skip(reason)无条件跳过被装饰的测试方法;

@unittest.skipIf(condition,reason)如果条件为真,则继续执行执行,否则跳过被装饰的测试用例;

@unittest.skipUnless(condition,reason)除非条件为真,否则跳过被装饰的测试用例;

@unittest.expectedFailure将测试用例标记为“预期失败”:

但是如果需要判断上一个测试方法是否执行成功再执行下一个,比如登陆失败,则不用执行下面的。skipif方法有点捉襟见肘。需要自己写装饰器,当然也可以在第二个方法里面直接获取第一个的执行结i果,如果失败,则不执行步骤

装饰器实现方法:(来源于网上)

#coding=utf-8

import unittest

def skipTest(value):

    def deco(function):

        def wrapper(self,*args,**kw):

            if not getattr(self, value):

                self.skipTest('login fail,skip test')

else:

                function(self,*args,**kwargs)

return wrapper

return deco

class MyTestCase(unittest.TestCase):

    loginflag=True

    def test_a_login(self):

      #如果登陆失败,则返回loginflag为false

        MyTestCase.loginflag=False

    @skipTest('loginflag')

def test_b_search(self):

        print "b"

if __name__== '__main__':

    unittest.main()

你可能感兴趣的:(python-unittest跳过测试方法)