如何在中间终止python 2 doctest文件?

mit*_*mit 5 python doctest scenarios

有时,仅运行大型doctests文件的第一部分会很有用.

在代码更改后第一部分中断的情况很多,我想只运行第一部分,直到它通过,然后再次运行整个文件.

我还没有找到一个简单的方法来做到这一点.

假设我用这个文件开始我的doctests:

#!/usr/bin/env python
import doctest
doctest.testfile("scenario.rst")
Run Code Online (Sandbox Code Playgroud)

而scenario.rst看起来像这样:

>>> 'hello world'
'hello world'

>>> exit()

>>> 'this should not be processed anymore'

... lots of lines

>>> 'this should also not be processed'
Run Code Online (Sandbox Code Playgroud)

在这个例子中,我使用exit()函数来演示我的意思,当然它不起作用,因为它被视为异常,doctest愉快地将其视为可以测试的一部分:

**********************************************************************
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.
Run Code Online (Sandbox Code Playgroud)

那么这样一个doctest文件怎么能在中间被终止呢?

编辑:有+ SKIP指令,但它只跳过一行.我需要一些能够跳过文件其余部分的内容.

jal*_*anb 3

>>> raise KeyboardInterrupt
Run Code Online (Sandbox Code Playgroud)

与所有其他异常不同,这将在任何时候停止Doctest

就我个人而言,我认为 KeyboardInterrupt 异常对于 doctest 来说就像 SystemExit 异常对于 Python 的其余部分一样。