擅长:python、mysql、java
<p><code>assertRaises</code>用作上下文管理器:</p>
<pre><code>def test_validate_percent(self):
with self.assertRaises(ValidationError):
validate_percent(1000)
</code></pre>
<p>或者用一个可呼叫的:</p>
<pre><code>def test_validate_percent(self):
self.assertRaises(ValidationError, validate_percent, 1000)
</code></pre>
<ul>
<li><a href="http://docs.python.org/2/library/unittest.html#unittest.TestCase.assertRaises" rel="noreferrer">http://docs.python.org/2/library/unittest.html#unittest.TestCase.assertRaises</a></li>
</ul>