How to end python 2 approval file in the middle?

Sometimes it is useful to run only the first part of a large doctrine file.

There are many situations where the first part breaks after changing the code, I would like to run only the first part until it passes, and then run the entire file again.

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

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

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

And the .rst script 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, this will not work, because it is considered as an exception, which the doctrine happily sees as part of what it can check:

 ********************************************************************** 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 can you complete such a dossier file in the middle?

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

+4
source share
3 answers
 >>> raise KeyboardInterrupt 

This will stop Doctest at any time, unlike all other exceptions.

Personally, I think the KeyboardInterrupt exception is a doctrine, since the SystemExit exception applies to the rest of Python.

+2
source

Here is what I do: I insert

 >>> 'ERROR' 

in the place where I want to stop the doctest file, and then I ask my test runner to turn on the doctest.REPORT_ONLY_FIRST_FAILURE flag (with zope.testrunner it bin/test -1 ).

Perhaps it would be enough to do

 >>> 'ERROR' # doctest: +REPORT_ONLY_FIRST_FAILURE 

inside your doctest file.

Personally, I don't like doctest.testfile . I prefer to create doctest.DocFileSuite() , combine them into unittest.TestSuite() , and then run them all with unittest.TextTestRunner() or something like that. And I usually add optionflags=doctest.REPORT_ONLY_FIRST_FAILURE when I create DocFileSuite objects, since I really like this parameter.

+4
source

According to this error report , there are currently 2 workarounds:

  • Replace β†’> with ">
  • separate the docstring and add something like " dont_test =" before the second part; the string becomes part of the instruction and will not be parsed.
0
source

Source: https://habr.com/ru/post/1402234/


All Articles