Search code examples
pythondoctestscenarios

How to terminate a python 2 doctest file in the middle?


Sometimes it can be useful to run only the first part of a large doctests file.

There are many situations when the first part breaks after a code change, I would like to run only the first part, until it passes, and then run the whole file again.

I could not yet find an easy way to do this.

Let's say I start my doctests with this file:

#!/usr/bin/env python
import doctest
doctest.testfile("scenario.rst")

And scenario.rst looks like this:

>>> 'hello world'
'hello world'

>>> exit()

>>> 'this should not be processed anymore'

... lots of lines

>>> 'this should also not be processed'

In this example I use the exit() function, to demonstrate what I mean, of course it does not work, because it is treated as an exception, which doctest happily sees as part of something it could test:

**********************************************************************
File "_scenario.rst", line 10, in _scenario.rst
Failed example:
    exit()
Exception raised:
    Traceback (most recent call last):
      File "c:\Python27\lib\doctest.py", line 1254, in __run
        compileflags, 1) in test.globs
      File "<doctest _scenario.rst[1]>", line 1, in <module>
        exit()
      File "c:\Python27\lib\site.py", line 372, in __call__
        raise SystemExit(code)
    SystemExit: None
**********************************************************************
File "_scenario.rst", line 12, in _scenario.rst
Failed example:
    'this should not be processed anymore'
Expected nothing
Got:
    'this should not be processed anymore'
**********************************************************************
1 items had failures:
   2 of   3 in _scenario.rst
***Test Failed*** 2 failures.

So how could such a doctest file be terminated in the middle?

EDIT: There is the +SKIP directive, but it skips only one line. I need something that skips the rest of the file.


Solution

  • >>> raise KeyboardInterrupt
    

    This will stop a Doctest at any point, unlike all other exceptions

    Personally, I consider that a KeyboardInterrupt exception is to a doctest as a SystemExit exception is to the rest of Python.