unittest系列分享:
我们在写用例的时候,我们需要写断言,那么我们是否要了解下,里面有什么断言可以使用呢,今天我们在这里分享下。
方法 | 检查 |
---|---|
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) |
简单举例
self.assertIsInstance(True, bool,msg="1不等于2")self.assertNotIsInstance(True,int)self.assertIn(1,[1,2])self.assertNotIn(1, [2,3])self.assertEqual(1, 1)self.assertNotEqual(1, 2)self.assertTrue(2==3)
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和b相同元素 |
简单举例
self.assertGreater(1,-1)self.assertGreaterEqual(2,1)self.assertNotRegex("1","122")self.assertCountEqual("12","1212")
方法 | 用于比较 |
---|---|
assertMultiLineEqual(a, b) | strings |
assertSequenceEqual(a, b) | sequences |
assertListEqual(a, b) | lists |
assertTupleEqual(a, b) | tuples |
assertSetEqual(a, b) | sets or frozensets |
assertDictEqual(a, b) | dicts |
举例
self.assertDictEqual({"code":1},{"code":1})self.assertListEqual([1,2],[1,'1'])self.assertMultiLineEqual("name","name")
后记
欢迎关注雷子说测试开发,后续将会持续为大家分享更多的技术知识,分享,点赞,在看--三连。
如果你有问题可以留言或者加我微信。2021,一起牛转钱坤,一起牛逼。