<p>Is it possible to write a <code>doctest</code> 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 <code>doctest</code> for that?</p>

[{'answer_id': 12609, 'author': 'cnu', 'author_id': 1448, 'author_profile': 'https://Stackoverflow.com/users/1448', 'pm_score': 8, 'selected': True, 'text': '<p>Yes. You can do it. The <a href="https://docs.python.org/3/library/doctest.html" rel="noreferrer">doctest module documentation</a> and Wikipedia has an <a href="http://en.wikipedia.org/wiki/Doctest#Example_2:_doctests_embedded_in_a_README.txt_file" rel="noreferrer">example</a> of it.</p>\n\n<pre><code>   &gt;&gt;&gt; x\n   Traceback (most recent call last):\n     ...\n   NameError: name \'x\' is not defined\n</code></pre>\n'}, {'answer_id': 33787471, 'author': 'glickind', 'author_id': 5577962, 'author_profile': 'https://Stackoverflow.com/users/5577962', 'pm_score': 3, 'selected': False, 'text': '<pre><code>&gt;&gt;&gt; import math\n&gt;&gt;&gt; math.log(-2)\nTraceback (most recent call last):\n ...\nValueError: math domain error\n</code></pre>\n\n<p>ellipsis flag <em># doctest: +ELLIPSIS</em> is not required to use ... in Traceback doctest </p>\n'}, {'answer_id': 63539385, 'author': 'runsun', 'author_id': 2565778, 'author_profile': 'https://Stackoverflow.com/users/2565778', 'pm_score': 4, 'selected': False, 'text': "<pre><code>&gt;&gt;&gt; scope # doctest: +IGNORE_EXCEPTION_DETAIL\nTraceback (most recent call last):\nNameError: name 'scope' is not defined\n</code></pre>\n<p>Don't know why the previous answers don't have the IGNORE_EXCEPTION_DETAIL. I need this for it to work. Py versioin: 3.7.3.</p>\n"}]
['https://Stackoverflow.com/questions/12592', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/742/']