UnitTest 框架 - 断言
Python 测试框架使用 Python 的内置 assert() 函数来测试特定条件。如果断言失败,则会引发 AssertionError。然后测试框架会将测试标识为失败。其他异常将被视为错误。
unittest 模块 − 中定义了以下三组断言函数
- 基本布尔断言
- 比较断言
- 集合断言
基本断言函数评估操作的结果是 True 还是 False。所有断言方法都接受 msg 参数,如果指定,则用作失败时的错误消息。
| Sr.No. | 方法和说明 |
|---|---|
| 1 | assertEqual(arg1, arg2, msg = None) 测试 arg1 和 arg2 是否相等。如果值不相等,测试将失败。 |
| 2 | assertNotEqual(arg1, arg2, msg = None) 测试 arg1 和 arg2 不相等。如果值相等,测试将失败。 |
| 3 | assertTrue(expr, msg = None) 测试 expr 是否为真。如果为假,测试失败 |
| 4 | assertFalse(expr, msg = None) 测试 expr 是否为假。如果为真,则测试失败 |
| 5 | assertIs(arg1, arg2, msg = None) 测试 arg1 和 arg2 是否计算为同一个对象。 |
| 6 | assertIsNot(arg1, arg2, msg = None) 测试 arg1 和 arg2 是否不计算为同一个对象。 |
| 7 | assertIsNone(expr, msg = None) 测试 expr 是否为 None。如果不是 None,则测试失败 |
| 8 | assertIsNotNone(expr, msg = None) 测试 expr 是否不为 None。如果为 None,则测试失败 |
| 9 | assertIn(arg1, arg2, msg = None) 测试 arg1 是否在 arg2 中。 |
| 10 | assertNotIn(arg1, arg2, msg = None) 测试 arg1 是否不在 arg2 中。 |
| 11 | assertIsInstance(obj, cls, msg = None) 测试 obj 是否是 cls 的一个实例> |
| 12 | assertNotIsInstance(obj, cls, msg = None) 测试 obj 是否不是 cls 的一个实例> |
上述部分断言函数在以下代码中实现 −
import unittest
class SimpleTest(unittest.TestCase):
def test1(self):
self.assertEqual(4 + 5,9)
def test2(self):
self.assertNotEqual(5 * 2,10)
def test3(self):
self.assertTrue(4 + 5 == 9,"The result is False")
def test4(self):
self.assertTrue(4 + 5 == 10,"assertion fails")
def test5(self):
self.assertIn(3,[1,2,3])
def test6(self):
self.assertNotIn(3, range(5))
if __name__ == '__main__':
unittest.main()
运行上述脚本时,测试2、测试4和测试6将显示失败,其他测试则运行成功。
FAIL: test2 (__main__.SimpleTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Python27\SimpleTest.py", line 9, in test2
self.assertNotEqual(5*2,10)
AssertionError: 10 == 10
FAIL: test4 (__main__.SimpleTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Python27\SimpleTest.py", line 13, in test4
self.assertTrue(4+5==10,"assertion fails")
AssertionError: assertion fails
FAIL: test6 (__main__.SimpleTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Python27\SimpleTest.py", line 17, in test6
self.assertNotIn(3, range(5))
AssertionError: 3 unexpectedly found in [0, 1, 2, 3, 4]
----------------------------------------------------------------------
Ran 6 tests in 0.001s
FAILED (failures = 3)
第二组断言函数是比较断言 −
assertAlmostEqual (first, second, places = 7, msg = None, delta = None)
通过计算差值、四舍五入到给定的小数位(默认 7),测试 first 和 second 是否近似(或不近似)相等,
assertNotAlmostEqual (first, second, places, msg, delta)
通过计算差值、四舍五入到给定的小数位(默认 7),并与零进行比较,测试 first 和 second 是否不近似相等。
在上述两个函数中,如果提供的是 delta 而不是 places,则 first 和 second 之间的差值必须小于或等于(或大于)delta。
同时提供 delta 和 places 会引发 TypeError。
assertGreater (first, second, msg = None)
根据方法名称测试 first 是否大于 second。否则,测试将失败。
assertGreaterEqual (first, second, msg = None)
根据方法名称测试 first 是否大于或等于 second。否则,测试将失败
assertLess (first, second, msg = None)
根据方法名称测试 first 是否小于 second。如果不是,测试将失败
assertLessEqual (first, second, msg = None)
根据方法名称测试 first 是否小于或等于 second。如果不是,测试将失败。
assertRegexpMatches (text, regexp, msg = None)
测试正则表达式搜索是否与文本匹配。如果失败,错误消息将包含模式和文本。 regexp 可以是正则表达式对象或包含适合 re.search() 使用的正则表达式的字符串。
assertNotRegexpMatches (text, regexp, msg = None)
验证 regexp 搜索与 text 不匹配。失败并显示错误消息,其中包括模式和匹配的 text 部分。regexp 可以是正则表达式对象或包含适合 re.search() 使用的正则表达式的字符串。
断言函数在以下示例中实现 −
import unittest
import math
import re
class SimpleTest(unittest.TestCase):
def test1(self):
self.assertAlmostEqual(22.0/7,3.14)
def test2(self):
self.assertNotAlmostEqual(10.0/3,3)
def test3(self):
self.assertGreater(math.pi,3)
def test4(self):
self.assertNotRegexpMatches("Tutorials Point (I) Private Limited","Point")
if __name__ == '__main__':
unittest.main()
上述脚本将 test1 和 test4 报告为失败。在 test1 中,22/7 的除法不在 3.14 的 7 位小数之内。同样,由于第二个参数与第一个参数中的文本匹配,因此 test4 导致 AssertionError。
=====================================================FAIL: test1 (__main__.SimpleTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "asserttest.py", line 7, in test1
self.assertAlmostEqual(22.0/7,3.14)
AssertionError: 3.142857142857143 != 3.14 within 7 places
================================================================
FAIL: test4 (__main__.SimpleTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "asserttest.py", line 13, in test4
self.assertNotRegexpMatches("Tutorials Point (I) Private Limited","Point")
AssertionError: Regexp matched: 'Point' matches 'Point' in 'Tutorials Point (I)
Private Limited'
----------------------------------------------------------------------
Ran 4 tests in 0.001s
FAILED (failures = 2)
集合断言
这组断言函数旨在与 Python 中的集合数据类型一起使用,例如 List、Tuple、Dictionary 和 Set。
| Sr.No. | 方法和说明 |
|---|---|
| 1 | assertListEqual (list1, list2, msg = None) 测试两个列表是否相等。如果不是,则构造一条错误消息,仅显示两者之间的差异。 |
| 2 | assertTupleEqual (tuple1, tuple2, msg = None) 测试两个元组是否相等。如果不是,则构造一条错误消息,仅显示两者之间的差异。 |
| 3 | assertSetEqual (set1, set2, msg = None) 测试两个集合是否相等。如果不是,则构造一个错误消息,列出集合之间的差异。 |
| 4 | assertDictEqual (expected, actual, msg = None) 测试两个字典是否相等。如果不相等,则构造一个错误消息,显示字典中的差异。 |
以下示例实现了上述方法 −
import unittest
class SimpleTest(unittest.TestCase):
def test1(self):
self.assertListEqual([2,3,4], [1,2,3,4,5])
def test2(self):
self.assertTupleEqual((1*2,2*2,3*2), (2,4,6))
def test3(self):
self.assertDictEqual({1:11,2:22},{3:33,2:22,1:11})
if __name__ == '__main__':
unittest.main()
在上面的例子中,test1和test3都出现了AssertionError错误,错误信息显示了List和Dictionary对象的差异。
FAIL: test1 (__main__.SimpleTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "asserttest.py", line 5, in test1
self.assertListEqual([2,3,4], [1,2,3,4,5])
AssertionError: Lists differ: [2, 3, 4] != [1, 2, 3, 4, 5]
First differing element 0:
2
1
Second list contains 2 additional elements.
First extra element 3:
4
- [2, 3, 4]
+ [1, 2, 3, 4, 5]
? +++ +++
FAIL: test3 (__main__.SimpleTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "asserttest.py", line 9, in test3
self.assertDictEqual({1:11,2:22},{3:33,2:22,1:11})
AssertionError: {1: 11, 2: 22} != {1: 11, 2: 22, 3: 33}
- {1: 11, 2: 22}
+ {1: 11, 2: 22, 3: 33}
? +++++++
----------------------------------------------------------------------
Ran 3 tests in 0.001s
FAILED (failures = 2)

