O'Reilly logo

Python Testing Beginner's Guide by Daniel Arbuckle

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Time for action - expecting an exception

This is yet another test that you can add to test.txt, this time testing some code that ought to raise an exception.

  1. 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)
    
  2. 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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required