1. 小数位模糊等于
自动化脚本最重要的是断言,正确设置断言以后才能帮助我们判断测试用例执行结果。
在小同事的帮助下,整理了一份比较详细的断言方法。
一、先说说unittest常用的断言吧
常用的就以下几个,网上一搜一大堆。python版本2.7以上都可以调用了。
断言语法 | 解释 |
(默认为7),如果在place位以内不同则断言失败。 | |
assertDictContainsSubset(expected, actual[, msg]) |
个多行字符串是相等的 |
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. |
三、在unittest包里面看到的比较全的断言