unittest断言

在测试用例中,执行完测试用例后,最后一步是判断测试结果是pass还是fail,自动化测试脚本里面一般把这种生成测试结果的方法称为断言(assert)
基础断言:
Method Checks that
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)
assertNotIsInstance(a, b) not isinstance(a, b)
assertAlmostEqual(a, b) a,b在规定的小数位是否约等于
assertNotAlmostEqual(a, b) a,b在规定的小数位是否不约等于
assertGreater(a, b) a > b
assertGreaterEqual(a, b) a >= b
assertLess(a, b) a < b
assertLessEqual(a, b) a <= b
assertRegexpMatches(s, r) r.search(s)
assertNotRegexpMatches(s, r) not r.search(s)
assertItemsEqual(a, b) sorted(a) == sorted(b) and works with unhashable objs

实例如下:

第34节:unittest断言

第34节:unittest断言

复杂断言:

序号 断言方法 断言描述
1 assertListEqual (list1, list2, msg = None) 验证列表list1、list2相等,不等则fail,同时报错信息返回具体的不同的地方
2 assertTupleEqual (tuple1, tuple2, msg = None) 验证元组tuple1、tuple2相等,不等则fail,同时报错信息返回具体的不同的地方
3 assertSetEqual (set1, set2, msg = None) 验证集合set1、set2相等,不等则fail,同时报错信息返回具体的不同的地方
4 assertDictEqual (expected, actual, msg = None) 验证字典expected、actual相等,不等则fail,同时报错信息返回具体的不同的地方
举例如下:

第34节:unittest断言

相关文章:

  • 2021-12-30
  • 2022-01-08
  • 2022-03-05
  • 2022-12-23
  • 2022-12-23
  • 2021-12-13
  • 2021-09-15
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-09-05
  • 2022-01-18
  • 2021-12-20
  • 2022-12-23
  • 2021-09-03
相关资源
相似解决方案