UnitTest 框架 – 断言
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是否为真。如果为 false,则测试失败 |
| 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。如果没有,测试失败 |
| 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()
当上面的脚本运行时,test2、test4和test6会显示失败,其他运行成功。
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(第一,第二,位置 = 7,味精 = 无,增量 = 无)
测试第一和第二近似(或不近似)通过计算差,四舍五入到小数点的给定数目等于地方(默认7),
-
assertNotAlmostEqual (first, second, places, msg, delta)
通过计算差值、四舍五入到给定的小数位数(默认为 7)并与零进行比较来测试第一个和第二个是否近似相等。
在上述两个函数中,如果提供了 delta 而不是地方,那么第一个和第二个之间的差异必须小于或等于(或大于)delta。
提供 delta 和 place 会引发 TypeError。
-
assertGreater(第一,第二,味精=无)
根据方法名称测试first是否大于second。否则,测试将失败。
-
assertGreaterEqual(第一,第二,味精=无)
根据方法名称测试first是否大于或等于second。如果没有,测试将失败
-
assertLess(第一,第二,味精=无)
根据方法名称测试first小于second。如果没有,测试将失败
-
assertLessEqual(第一,第二,味精=无)
根据方法名称测试first是否小于或等于second。否则,测试将失败。
-
assertRegexpMatches (text, regexp, msg = None)
测试正则表达式搜索是否与文本匹配。如果失败,错误消息将包括模式和文本。regexp 可以是一个正则表达式对象,也可以是一个包含适合re.search()使用的正则表达式的字符串。
-
assertNotRegexpMatches (text, regexp, msg = None)
验证正则表达式搜索与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)