This is yet another test that you can add to test.txt
, this time testing some code that ought to raise an exception.
Insert the following text into your doctest file (Please note that the last line of this text has been wrapped due to the constraints of the book's format, and should be a single line):
Here we use doctest's exception syntax to check that Python is correctly enforcing its grammar. >>> def faulty(): ... yield 5 ... return 7 Traceback (most recent call last): SyntaxError: 'return' with argument inside generator (<doctest test.txt[5]>, line 3)
The test is supposed to raise an exception, so it will fail if it doesn't raise the exception, or if it raises the wrong exception. Make sure you have your mind wrapped around that: if the test code executes successfully, the test fails, because it expected an exception.
Run the tests using doctest and the following screen will be displayed: