Can you check that an exception is thrown with doctest in Python?
<p>Is it possible to write a doctest unit test that will check that an exception is raised?<br>
For example, if I have a function <code>foo(x)</code> that is supposed to raise an exception if <code>x&lt;0</code>, how would I write the doctest for that? </p>

<p>Yes. You can do it. The <a href="https://docs.python.org/3/library/doctest.html">doctest module documentation</a> and Wikipedia has an <a href="http://en.wikipedia.org/wiki/Doctest#Example_2%3a_doctests_embedded_in_a_README.txt_file">example</a> of it.</p>

<pre><code>   &gt;&gt;&gt; x
   Traceback (most recent call last):
     ...
   NameError: name 'x' is not defined
</code></pre>
