Python的unittest框架的断言总结
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Python的unittest框架的断言总结,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3151字,纯文字阅读大概需要5分钟。
内容图文
常用的断言方法如下:
assertFalse:为假时返回True;self.assertFalse(表达式,“表达式为true时打印的message”)
assertTrue:为真时返回True;self.assertTrue(表达式,“表达式为false时打印的message”)
asserEqual:相等返回True;self.assertEqual(表达式1,表达式2,“表达式1不等于表达式2时打印的message”)
assertNotEqual:不相等返回True;self.assertNotEqual(表达式1,表达式2,“表达式1等于表达式2时打印的message”)
assertIsNone:不存在,则pass;self.assertIsNone(表达式,“如果表达存在打印的message”)
assertIsNotNone:存在,则pass;self.assertIsNotNone(表达式,“如果表达式不存在打印的message”)
在官方文档里面看到的整理一下,有些还来不及翻译。
assertAlmostEqual(first, second[, places, ...]) |
适用于小数,place是应最少几位相等布尔值才为1(默认为7),如果在place位以内不同则断言失败。 |
assertDictContainsSubset(expected, actual[, msg]) |
检查实际是否超预期 |
assertDictEqual(d1, d2[, msg]) |
前后字典是否相同 |
assertEqual(first, second[, msg]) |
前后两个数不等的话,失败 |
assertFalse(expr[, msg]) |
检查表达式是否为假 |
assertGreater(a, b[, msg]) |
和self.assertTrue(a > b)用法一样,但是多了设置条件 . |
assertGreaterEqual(a, b[, msg]) |
和self.assertTrue(a > =b)用法一样,但是多了设置条件 . |
assertIn(member, container[, msg]) |
self.assertTrue(a in b) |
assertIs(expr1, expr2[, msg]) |
assertTrue(a is b) |
assertIsInstance(obj, cls[, msg]) |
Isinstance(a,b) |
assertIsNone(obj[, msg]) |
Obj is none. |
assertIsNot(expr1, expr2[, msg]) |
a is not b. |
assertIsNotNone(obj[, msg]) |
Obj is not none. |
assertItemsEqual(expected_seq, actual_seq[, msg]) |
一个无序的序列特异性的比较。 |
assertLess(a, b[, msg]) |
Just like self.assertTrue(a < b), but with a nicer default message. |
assertLessEqual(a, b[, msg]) |
Just like self.assertTrue(a <= b), but with a nicer default message. |
assertListEqual(list1, list2[, msg]) |
List1与list2是否相等. |
assertMultiLineEqual(first, second[, msg]) |
断言,2个多行字符串是相等的 |
assertNotAlmostEqual(first, second[, ...]) |
Fail if the two objects are equal as determined by their difference rounded to the given number of decimal places (default 7) and comparing to zero, or by comparing that the between the two objects is less than the given delta. |
assertNotAlmostEquals(first, second[, ...]) |
Fail if the two objects are equal as determined by their difference rounded to the given number of decimal places (default 7) and comparing to zero, or by comparing that the between the two objects is less than the given delta. |
assertNotEqual(first, second[, msg]) |
Fail if the two objects are equal as determined by the ‘==’ |
assertNotEquals(first, second[, msg]) |
Fail if the two objects are equal as determined by the ‘==’ |
assertNotIn(member, container[, msg]) |
Just like self.assertTrue(a not in b), but with a nicer default message. |
assertNotIsInstance(obj, cls[, msg]) |
Included for symmetry with assertIsInstance. |
assertNotRegexpMatches(text, unexpected_regexp) |
如果文本匹配正则表达式,将失败。 |
assertRaises(excClass[, callableObj]) |
除非excclass类抛出异常失败 |
assertRaisesRegexp(expected_exception, ...) |
认为在引发异常的情况下消息匹配一个正则表达式。 |
assertRegexpMatches(text, expected_regexp[, msg]) |
测试失败,除非文本匹配正则表达式。 |
assertSequenceEqual(seq1, seq2[, msg, seq_type]) |
有序序列的相等断言 (like lists and tuples). |
assertSetEqual(set1, set2[, msg]) |
A set-specific equality assertion. |
assertTrue(expr[, msg]) |
Check that the expression is true. |
assertTupleEqual(tuple1, tuple2[, msg]) |
A tuple-specific equality assertion. |
assert_(expr[, msg]) |
Check that the expression is true. |
原文:https://www.cnblogs.com/cindy-cindy/p/8243785.html
内容总结
以上是互联网集市为您收集整理的Python的unittest框架的断言总结全部内容,希望文章能够帮你解决Python的unittest框架的断言总结所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。