unittest的TestCase類提供的判斷方法
阿新 • • 發佈:2018-11-03
unittest的TestCase類提供的判斷方法
方法 | 用法 |
---|---|
assertEqual(a, b) | 判斷a==b |
assertNotEqual(a, b) | 判斷a!=b |
assertTrue(x) | bool(x) is True |
assertFalse(x) | bool(x) is False |
assertIs(a, b) | a is b |
assertIsNot(a, b) | a is not b |
assertIsNone(x) | x is None |
assertIsNotNone(x) | x is not None |
assertIn(a, b) | a in b |
assertNotIn(a, b) | a not in b |
assertIsInstance(a, b) | isinstance(a, b) 斷言a是b的一個例項 |
assertNotIsInstance(a, b) | not isinstance(a, b) 斷言a不是b的例項 |
檢查日誌、訊息、警告的生成的方法:
方法 | 用途 |
---|---|
assertRaises(exc, fun, *args, **kwds) | fun(*args, **kwds) raises exc |
assertRaisesRegex(exc, r, fun, *args, **kwds) | fun(*args, **kwds) raises exc and the message matches regex r |
assertWarns(warn, fun, *args, **kwds) | fun(*args, **kwds) raises warn |
assertWarnsRegex(warn, r, fun, *args, **kwds) | fun(*args, **kwds) raises warn and the message matches regex r |
assertLogs(logger, level) | The with block logs on logger with minimum level |
一些其他常用方法:
Method | Checks that |
---|---|
assertAlmostEqual(a, b) | round(a-b, 7) == 0 |
assertNotAlmostEqual(a, b) | round(a-b, 7) != 0 |
assertGreater(a, b) | a > b |
assertGreaterEqual(a, b) | a >= b |
assertLess(a, b) | a < b |
assertLessEqual(a, b) | a <= b |
assertRegex(s, r) | r.search(s) |
assertNotRegex(s, r) | not r.search(s) |
assertCountEqual(a, b) | a and b have the same elements in the same number, regardless of their order |