python+unittest跳过测试和预期失败
在运行测试时,有时需要直接跳过某些测试用例,或者当测试用例符合某个条件时跳 过测试,又或者直接将测试用例设置为失败。、
import unittest from leap_year import LeapYear class TestLeapYear(unittest.TestCase): @unittest.skip("直接跳过测试") def test_2000(self): ly = LeapYear(2000) self.assertEqual(ly.answer(), '20000是闰年') @unittest.skipIf(3 > 2, "当条件为真时跳过测试") def test_2004(self): ly = LeapYear(2004) self.assertEqual(ly.answer(), '2004是闰年') # 当条件为真时,则执行装饰的测试 @unittest.skipUnless(3 > 2, "当条件为真时执行测试") def test_2017(self): ly = LeapYear(2017) self.assertEqual(ly.answer(), '2017不是闰年') # 不管执行结果是否失败,都将测试标记为失败 @unittest.expectedFailure def test_2100(self): ly = LeapYear(2100) self.assertEqual(ly.answer(), '2100不是闰年') if __name__ == '__main__': unittest.main()