大きな doctests ファイルの最初の部分だけを実行すると便利な場合があります。
コードの変更後に最初の部分が壊れる状況はたくさんあります。最初の部分だけを実行し、それが通るまで実行してから、ファイル全体を再度実行したいと思います。
これを行う簡単な方法をまだ見つけることができませんでした。
このファイルで doctests を開始するとしましょう:
#!/usr/bin/env python
import doctest
doctest.testfile("scenario.rst")
そして、scenario.rst は次のようになります。
>>> 'hello world'
'hello world'
>>> exit()
>>> 'this should not be processed anymore'
... lots of lines
>>> 'this should also not be processed'
この例では、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.
では、そのような doctest ファイルを途中で終了するにはどうすればよいでしょうか?
編集: +SKIP ディレクティブがありますが、1 行だけスキップします。ファイルの残りをスキップするものが必要です。