4.unittest-自动跳过测试用例

unittest中提供了一些跳过用例的装饰器方法

1、@unittest.skip(reason) 无条件跳过用例

import unittest

class TestCan(unittest.TestCase):

    # skip无条件跳过
    @unittest.skip("跳过测试用例test_01")
    def test_01(self):
        print("hello world1")

    def test_02(self):
        print("hello world2")
        
if __name__ == '__main__':
    unittest.main(verbosity=2)    

执行结果
4.unittest-自动跳过测试用例_第1张图片

2、@unittest.skipIf(condition, reason) 如果条件为真,则跳过测试。

import unittest

class TestCan(unittest.TestCase):
    
    # skip无条件跳过
    @unittest.skip("跳过测试用例test_01")
    def test_01(self):
        print("hello world1")

    def test_02(self):
        print("hello world2")
    
    @unittest.skipIf(True, "条件成立不执行")
    def test_03(self):
        print("hello world3")

if __name__ == '__main__':
    unittest.main(verbosity=2) 

执行结果

4.unittest-自动跳过测试用例_第2张图片

3、@unittest.skipUnless(condition, reason) 条件不满足时,跳过测试。

import unittest

class TestCan(unittest.TestCase):
    # skip无条件跳过
    @unittest.skip("跳过测试用例test_01")
    def test_01(self):
        print("hello world1")

    def test_02(self):
        print("hello world2")

    @unittest.skipIf(True, "条件满足跳过用例")
    def test_03(self):
        print("hello world3")

    @unittest.skipUnless(False,"条件不满足跳过用例")
    def test_04(self):
        print("test04")

if __name__ == '__main__':
    unittest.main(verbosity=2)

执行结果
4.unittest-自动跳过测试用例_第3张图片

4、跳过测试类

import unittest

@unittest.skip("跳跳跳")
class TestCan(unittest.TestCase):
    # skip无条件跳过
    # @unittest.skip("跳过测试用例test_01")
    def test_01(self):
        print("hello world1")

    def test_02(self):
        print("hello world2")

    # @unittest.skipIf(True, "条件满足跳过用例")
    def test_03(self):
        print("hello world3")

    # @unittest.skipUnless(False,"条件不满足跳过用例")
    def test_04(self):
        print("test04")

if __name__ == '__main__':
    unittest.main(verbosity=2)

执行结果为
4.unittest-自动跳过测试用例_第4张图片

你可能感兴趣的:(unittest,unittest)