发布于 2015-08-30 07:53:58 | 117 次阅读 | 评论: 0 | 来源: 网络整理
To test for exceptions, use the assertRaises() method. For example, if you want to test that a function raised a ValueError exception, use this code:
import unittest
# A simple function to illustrate def parse_int(s):
return int(s)
If you need to test the exception’s value in some way, then a different approach is needed. For example:
import errno
The assertRaises() method provides a convenient way to test for the presence of an exception. A common pitfall is to write tests that manually try to do things with excep‐ tions on their own. For instance:
The problem with such approaches is that it is easy to forget about corner cases, such as that when no exception is raised at all. To do that, you need to add an extra check for that situation, as shown here:
The assertRaises() method simply takes care of these details, so you should prefer to use it. The one limitation of assertRaises() is that it doesn’t provide a means for testing the value of the exception object that’s created. To do that, you have to manually test it, as shown. Somewhere in between these two extremes, you might consider using the as sertRaisesRegex() method, which allows you to test for an exception and perform a regular expression match against the exception’s string representation at the same time. For example:
A little-known fact about assertRaises() and assertRaisesRegex() is that they can also be used as context managers:
This form can be useful if your test involves multiple steps (e.g., setup) besides that of simply executing a callable.