Can you check that an exception is thrown with doctest in Python? Can you check that an exception is thrown with doctest in Python? python python

Can you check that an exception is thrown with doctest in Python?


Yes. You can do it. The doctest module documentation and Wikipedia has an example of it.

   >>> x   Traceback (most recent call last):     ...   NameError: name 'x' is not defined


>>> scope # doctest: +IGNORE_EXCEPTION_DETAILTraceback (most recent call last):NameError: name 'scope' is not defined

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.


>>> import math>>> math.log(-2)Traceback (most recent call last): ...ValueError: math domain error

ellipsis flag # doctest: +ELLIPSIS is not required to use ... in Traceback doctest