单元测试框架 - 断言
Python 测试框架使用Python 内置的assert() 函数来测试特定条件。如果断言失败,将引发 AssertionError。然后测试框架会将测试识别为失败。其他异常均视为错误。
以下三组断言函数在unittest模块中定义 -
- 基本布尔断言
- 比较断言
- 集合断言
基本断言函数评估运算结果是 True 还是 False。所有的断言方法都接受一个msg参数,如果指定的话,将用作失败时的错误消息。
| 先生。 | 方法及说明 |
|---|---|
| 1 | 断言Equal(arg1,arg2,msg =无) 测试arg1和arg2是否相等。如果值比较不相等,则测试将失败。 |
| 2 | 断言NotEqual(arg1,arg2,msg =无) 测试arg1和arg2是否不相等。如果这些值比较相等,则测试将失败。 |
| 3 | 断言True(表达式,味精=无) 测试expr是否为真。如果为 false,则测试失败 |
| 4 | 断言假(表达式,味精=无) 测试expr是否为假。如果为 true,则测试失败 |
| 5 | 断言Is(arg1,arg2,msg =无) 测试arg1和arg2是否计算为同一对象。 |
| 6 | 断言IsNot(arg1,arg2,msg =无) 测试arg1和arg2的计算结果是否为同一对象。 |
| 7 | 断言IsNone(表达式,味精=无) 测试expr是否为 None。如果不是 None,则测试失败 |
| 8 | 断言IsNotNone(expr, msg = None) 测试expr不是 None。如果没有,测试失败 |
| 9 | 断言(arg1,arg2,msg =无) 测试arg1是否在arg2中。 |
| 10 | 断言NotIn(arg1,arg2,msg =无) 测试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)
第二组断言函数是比较断言 -
断言AlmostEqual(第一,第二,位置= 7,消息=无,增量=无)
通过计算差值、四舍五入到给定的小数位数(默认为 7)来测试第一和第二是否近似(或不近似)相等,
assertNotAlmostEqual(第一、第二、位置、消息、增量)
通过计算差值、四舍五入到给定的小数位数(默认为 7)并与零进行比较,测试第一和第二是否近似相等。
在上述两个函数中,如果提供 delta 而不是地方,则第一个和第二个之间的差必须小于或等于(或大于)delta。
同时提供 delta 和 place 会引发类型错误。
assertGreater(第一,第二,msg = None)
根据方法名称测试第一个是否大于第二个。如果没有,测试将失败。
assertGreaterEqual(第一,第二,msg = None)
根据方法名称测试第一个是否大于或等于第二个。如果没有,测试将失败
assertLess(第一,第二,msg = None)
根据方法名称测试第一个是否小于第二个。如果没有,测试将失败
assertLessEqual(第一,第二,msg = None)
根据方法名称测试第一个是否小于或等于第二个。如果没有,测试将失败。
assertRegexpMatches(文本,正则表达式,msg = None)
测试正则表达式搜索是否与文本匹配。如果失败,错误消息将包括模式和文本。regexp 可以是正则表达式对象或包含适合re.search()使用的正则表达式的字符串。
断言NotRegexpMatches(文本,正则表达式,msg =无)
验证正则表达式搜索与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。
| 先生。 | 方法及说明 |
|---|---|
| 1 | 断言ListEqual(列表1,列表2,msg =无) 测试两个列表是否相等。如果不是,则会构造一条错误消息,仅显示两者之间的差异。 |
| 2 | assertTupleEqual(元组1,元组2,msg =无) 测试两个元组是否相等。如果不是,则会构造一条错误消息,仅显示两者之间的差异。 |
| 3 | assertSetEqual (set1, set2, msg = None) 测试两组是否相等。如果不是,则会构建一条错误消息,列出各组之间的差异。 |
| 4 | assertDictEqual(预期,实际,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)