unittest跳过执行测试用例
@
unittest.
skip
(reason) 跳过被此装饰器装饰的测试。 reason 为测试被跳过的原因。
@
unittest.
skipIf
(condition, reason) 当 condition 为真时,跳过被装饰的测试。
@
unittest.
skipUnless
(condition, reason) 跳过被装饰的测试,除非 condition 为真。
@
unittest.
expectedFailure
把测试标记为预计失败。如果测试不通过,会被认为测试成功;如果测试通过了,则被认为是测试失败。- 实例:
import unittest class Test_Study2(unittest.TestCase): def setUp(self): print('start======') def tearDown(self): print('end======') #强制跳过,跳过此用例 @unittest.skip('跳过此用例') def test_case1(self): print(" run test case 1111") def test_case2(self): print("run test case 2222") @unittest.skipIf(3 > 2, "如果条件为真,跳过此用例") def test_case3(self): print("run test case 3333") @unittest.skipUnless(3 < 2, "除非条件为真,否则跳过用例") def test_case4(self): print("run test case 4444") def test_case5(self): print("run test case 5555") if __name__ == "__main__": unittest.main()
执行结果:
start====== run test case 2222 end====== start====== run test case 5555 end====== ---------------------------------------------------------------------- Ran 5 tests in 0.001s OK (skipped=3) Process finished with exit code 0
针对@
unittest.
expectedFailure
的单独实例:
#运行通过,标记为失败 @unittest.expectedFailure def test_case5(self): self.assertEqual(1, 1) 运行结果 FAILED (unexpected successes=1) #运行不通过,标记为成功 @unittest.expectedFailure def test_case5(self): self.assertEqual(1, 0) 运行结果: OK (expected failures=1)