文件名称:忽略或者期望测试失败-python cookbook(第3版)高清中文完整版
文件大小:4.84MB
文件格式:PDF
更新时间:2024-06-29 23:06:51
python cookbook 第3版 高清 中文完整版
14.5 忽略或者期望测试失败 问题 You want to skip or mark selected tests as an anticipated failure in your unit tests. 解决方案 The unittest module has decorators that can be applied to selected test methods to control their handling. For example: import unittest import os import platform class Tests(unittest.TestCase): def test_0(self): self.assertTrue(True) @unittest.skip(‘skipped test’) def test_1(self): self.fail(‘should have failed!’) @unittest.skipIf(os.name==’posix’, ‘Not supported on Unix’) def test_2(self): import winreg @unittest.skipUnless(platform.system() == ‘Darwin’, ‘Mac specific test’) def test_3(self): self.assertTrue(True) @unittest.expectedFailure def test_4(self): self.assertEqual(2+2, 5) if __name__ == ‘__main__’: unittest.main() If you run this code on a Mac, you’ll get this output: