擅长:python、mysql、java
<p>我发现了一个上下文,在这个上下文中,捕获所有错误(只有一个错误)并不是坏事,即单元测试。</p>
<p>如果我有方法:</p>
<pre><code>def my_method():
try:
something()
except IOError, e:
handle_it()
</code></pre>
<p>然后,它可能有一个单元测试,看起来像:</p>
<pre><code>def test_my_method():
try:
my_module.my_method()
except IOError, e:
print "shouldn't see this error message"
assert False
except Exception, e:
print "some other error message"
assert False
assert True
</code></pre>
<p>因为你现在已经检测到我的方法抛出了一个意外的异常。</p>