跳过测试和预期失败
概要
- unittest.skip() 直接跳过测试
- unittest.skipIf() 条件为真,跳过测试
- unittest.skipUnless 条件为假,跳过测试
- unittest.expectedFailure 预期设置失败
import unittest
class Test1(unittest.TestCase):
@classmethod
def setUpClass(cls):
print('setUpClass')
print()
def setUp(self):
print("Test1 start")
#条件为真,跳过测试
# @unittest.skipIf(4 > 3, "skip Test_c")
def test_c(self):
print("test_c")
# 条件为假,跳过测试
# @unittest.skipUnless(1<0, "skip test_b")
def test_b(self):
print("test_b")
def tearDown(self):
print("Test1 end")
@classmethod
def tearDownClass(cls):
print('tearDownClass')
# 跳过Test2测试类 将不会被执行
@unittest.skip(" skip Test_2")
class Test2(unittest.TestCase):
def setUp(self):
print("Test2 start")
def test_d(self):
print("test_d")
# 预期设置失败
@unittest.expectedFailure
def test_a(self):
print("test_a")
def tearDown(self):
print("Test2 end!")
if __name__ == '__main__':
unittest.main()
输出结果:
setUpClass
Test1 start
test_b
Test1 end
Test1 start
test_c
Test1 end
tearDownClass