Unittest Framework 简明教程

UnitTest Framework - Exceptions Test

Python 测试框架提供以下断言方法来检查是否引发了异常。

Python testing framework provides the following assertion methods to check that exceptions are raised.

assertRaises(exception, callable, *args, **kwds)

测试当调用函数时抛出异常(第一个参数),则只要有位置或关键字参数。当抛出预期的异常时测试通过,如果抛出其他异常则报错,如果没有抛出异常则测试失败。为了捕获一组异常,可将包含异常类的元组作为异常传递。

Test that an exception (first argument) is raised when a function is called with any positional or keyword arguments. The test passes if the expected exception is raised, is an error if another exception is raised, or fails if no exception is raised. To catch any of a group of exceptions, a tuple containing the exception classes may be passed as exception.

在下面的示例中,定义一个测试函数来检查是否抛出 ZeroDivisionError。

In the example below, a test function is defined to check whether ZeroDivisionError is raised.

import unittest

def div(a,b):
   return a/b
class raiseTest(unittest.TestCase):
   def testraise(self):
      self.assertRaises(ZeroDivisionError, div, 1,0)

if __name__ == '__main__':
   unittest.main()

testraise() 函数使用 assertRaises() 函数查看当调用 div() 函数时是否会出现除零操作。上面的代码会抛出异常。但将 div() 函数的参数更改如下 −

The testraise() function uses assertRaises() function to see if division by zero occurs when div() function is called. The above code will raise an exception. But changes arguments to div() function as follows −

self.assertRaises(ZeroDivisionError, div, 1,1)

当使用这些更改运行代码时,测试失败,因为不会出现 ZeroDivisionError。

When a code is run with these changes, the test fails as ZeroDivisionError doesn’t occur.

F
================================================================
FAIL: testraise (__main__.raiseTest)
----------------------------------------------------------------------
Traceback (most recent call last):
   File "raisetest.py", line 7, in testraise
      self.assertRaises(ZeroDivisionError, div, 1,1)
AssertionError: ZeroDivisionError not raised

----------------------------------------------------------------------
Ran 1 test in 0.000s

FAILED (failures = 1)

assertRaisesRegexp(exception, regexp, callable, *args, **kwds)

测试 regexp 是否与抛出异常的字符串表示匹配。regexp 可以是正则表达式对象,或包含正则表达式的字符串,该正则表达式适合 re.search() 使用。

Tests that regexp matches on the string representation of the raised exception. regexp may be a regular expression object or a string containing a regular expression suitable for use by re.search().

以下示例演示如何使用 assertRaisesRegexp() −

The following example shows how assertRaisesRegexp() is used −

import unittest
import re

class raiseTest(unittest.TestCase):
   def testraiseRegex(self):
      self.assertRaisesRegexp(TypeError, "invalid", reg,"Point","TutorialsPoint")

if __name__ == '__main__':
   unittest.main()

在这里,testraseRegex() 测试不会失败,因为第一个参数是 “Point” 可以在第二个参数字符串中找到。

Here, testraseRegex() test doesn’t fail as first argument. "Point" is found in the second argument string.

================================================================
FAIL: testraiseRegex (__main__.raiseTest)
----------------------------------------------------------------------
Traceback (most recent call last):
   File "C:/Python27/raiseTest.py", line 11, in testraiseRegex
      self.assertRaisesRegexp(TypeError, "invalid", reg,"Point","TutorialsPoint")
AssertionError: TypeError not raised
----------------------------------------------------------------------

但是,更改如下所示 −

However, the change is as shown below −

self.assertRaisesRegexp(TypeError, "invalid", reg,123,"TutorialsPoint")

会抛出 TypeError 异常。因此,会显示以下结果 −

TypeError exception will be thrown. Hence, the following result will be displayed −

================================================================
FAIL: testraiseRegex (__main__.raiseTest)
----------------------------------------------------------------------
Traceback (most recent call last):
   File "raisetest.py", line 11, in testraiseRegex
      self.assertRaisesRegexp(TypeError, "invalid", reg,123,"TutorialsPoint")
AssertionError: "invalid" does not match
   "first argument must be string or compiled pattern"
----------------------------------------------------------------------