有时,只运行大型doctest文件的第一部分非常有用。
在许多情况下,当第一部分在代码更改后中断时,我希望只运行第一部分,直到它通过,然后再次运行整个文件。
我还找不到一种简单的方法来做这件事。
假设我从这个文件开始我的doctest:
#!/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文件怎么能在中间终止呢?
EDIT:有+SKIP指令,但它只跳过一行。我需要跳过文件其余部分的东西。
发布于 2013-08-22 23:36:45
>>> raise KeyboardInterrupt这个will stop在任何时候都是Doctest,这与所有其他异常不同
就我个人而言,我认为KeyboardInterrupt异常适用于文档测试,就像SystemExit异常适用于Python的其余部分一样。
发布于 2012-03-20 01:19:52
下面是我要做的:我插入
>>> 'ERROR'在我想要停止doctest文件的地方,然后我要求我的测试运行器启用doctest.REPORT_ONLY_FIRST_FAILURE标志(对于zope.testrunner,它是bin/test -1)。
也许这样做就足够了
>>> 'ERROR' # doctest: +REPORT_ONLY_FIRST_FAILURE在您的doctest文件中。
就我个人而言,我不喜欢doctest.testfile。我更喜欢创建一个doctest.DocFileSuite(),将它们组合成一个unittest.TestSuite(),然后用unittest.TextTestRunner()或类似的东西运行它们。我通常在创建DocFileSuite对象时将optionflags=doctest.REPORT_ONLY_FIRST_FAILURE添加到,因为我真的喜欢这个选项。
发布于 2012-03-19 21:50:26
根据this bug report的说法,目前有两种解决方法:
对文档字符串执行
https://stackoverflow.com/questions/9771213
复制相似问题