]> granicus.if.org Git - python/commitdiff
#16522: Add FAIL_FAST flag to doctest.
authorR David Murray <rdmurray@bitdance.com>
Wed, 21 Nov 2012 20:09:21 +0000 (15:09 -0500)
committerR David Murray <rdmurray@bitdance.com>
Wed, 21 Nov 2012 20:09:21 +0000 (15:09 -0500)
Patch by me, most of the work (doc and tests) by Daniel Urban.

Doc/library/doctest.rst
Doc/whatsnew/3.4.rst
Lib/doctest.py
Lib/test/test_doctest.py
Misc/NEWS

index 222c7195289ba2f0461fd90b9596ce62b07e2d30..8822ef3bae45d478c5179acb617fc5ccad3de61b 100644 (file)
@@ -633,6 +633,16 @@ The second group of options controls how test failures are reported:
    the output is suppressed.
 
 
+.. data:: FAIL_FAST
+
+   When specified, exit after the first failing example and don't attempt to run
+   the remaining examples. Thus, the number of failures reported will always be 1.
+   This flag may be useful during debugging, since examples after the first
+   failure won't even produce debugging output.
+
+   .. versionadded:: 3.4
+
+
 .. data:: REPORTING_FLAGS
 
    A bitmask or'ing together all the reporting flags above.
index 4bed2ed57197bce2002b75e72f6743b5aebfc78e..ab0b163d1b90e9cec33c290412fae6707a26aeaa 100644 (file)
@@ -150,7 +150,11 @@ New Modules
 Improved Modules
 ================
 
-* None yet.
+doctest
+-------
+
+Added ``FAIL_FAST`` flag to halt test running as soon as the first failure is
+detected.  (Contributed by R. David Murray and Daniel Urban in :issue:`16522`.)
 
 
 Optimizations
index 3af05fb87e65164ed1fee7ee17a4e1205f83d614..16a732d037846b29ad9e1f4fa02e69842a036b64 100644 (file)
@@ -62,6 +62,7 @@ __all__ = [
     'REPORT_NDIFF',
     'REPORT_ONLY_FIRST_FAILURE',
     'REPORTING_FLAGS',
+    'FAIL_FAST',
     # 1. Utility Functions
     # 2. Example & DocTest
     'Example',
@@ -150,11 +151,13 @@ REPORT_UDIFF = register_optionflag('REPORT_UDIFF')
 REPORT_CDIFF = register_optionflag('REPORT_CDIFF')
 REPORT_NDIFF = register_optionflag('REPORT_NDIFF')
 REPORT_ONLY_FIRST_FAILURE = register_optionflag('REPORT_ONLY_FIRST_FAILURE')
+FAIL_FAST = register_optionflag('FAIL_FAST')
 
 REPORTING_FLAGS = (REPORT_UDIFF |
                    REPORT_CDIFF |
                    REPORT_NDIFF |
-                   REPORT_ONLY_FIRST_FAILURE)
+                   REPORT_ONLY_FIRST_FAILURE |
+                   FAIL_FAST)
 
 # Special string markers for use in `want` strings:
 BLANKLINE_MARKER = '<BLANKLINE>'
@@ -1342,6 +1345,9 @@ class DocTestRunner:
             else:
                 assert False, ("unknown outcome", outcome)
 
+            if failures and self.optionflags & FAIL_FAST:
+                break
+
         # Restore the option flags (in case they were modified)
         self.optionflags = original_optionflags
 
index 8f8c7c7e82b11ba642e0fe2208df0d1f0dd6ceeb..3e36f1906f32841dddb7cbdec3ddcc66a86b8d51 100644 (file)
@@ -1409,8 +1409,40 @@ However, output from `report_start` is not suppressed:
         2
     TestResults(failed=3, attempted=5)
 
-For the purposes of REPORT_ONLY_FIRST_FAILURE, unexpected exceptions
-count as failures:
+The FAIL_FAST flag causes the runner to exit after the first failing example,
+so subsequent examples are not even attempted:
+
+    >>> flags = doctest.FAIL_FAST
+    >>> doctest.DocTestRunner(verbose=False, optionflags=flags).run(test)
+    ... # doctest: +ELLIPSIS
+    **********************************************************************
+    File ..., line 5, in f
+    Failed example:
+        print(2) # first failure
+    Expected:
+        200
+    Got:
+        2
+    TestResults(failed=1, attempted=2)
+
+Specifying both FAIL_FAST and REPORT_ONLY_FIRST_FAILURE is equivalent to
+FAIL_FAST only:
+
+    >>> flags = doctest.FAIL_FAST | doctest.REPORT_ONLY_FIRST_FAILURE
+    >>> doctest.DocTestRunner(verbose=False, optionflags=flags).run(test)
+    ... # doctest: +ELLIPSIS
+    **********************************************************************
+    File ..., line 5, in f
+    Failed example:
+        print(2) # first failure
+    Expected:
+        200
+    Got:
+        2
+    TestResults(failed=1, attempted=2)
+
+For the purposes of both REPORT_ONLY_FIRST_FAILURE and FAIL_FAST, unexpected
+exceptions count as failures:
 
     >>> def f(x):
     ...     r'''
@@ -1437,6 +1469,17 @@ count as failures:
         ...
         ValueError: 2
     TestResults(failed=3, attempted=5)
+    >>> flags = doctest.FAIL_FAST
+    >>> doctest.DocTestRunner(verbose=False, optionflags=flags).run(test)
+    ... # doctest: +ELLIPSIS
+    **********************************************************************
+    File ..., line 5, in f
+    Failed example:
+        raise ValueError(2) # first failure
+    Exception raised:
+        ...
+        ValueError: 2
+    TestResults(failed=1, attempted=2)
 
 New option flags can also be registered, via register_optionflag().  Here
 we reach into doctest's internals a bit.
index a56194c1097157492bd7822826217dbb475d59df..10a07cda24176915ee3a0419fe078e897fbb5005 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -138,6 +138,8 @@ Core and Builtins
 Library
 -------
 
+- Issue #16522: added FAIL_FAST flag to doctest.
+
 - Issue #15627: Add the importlib.abc.SourceLoader.compile_source() method.
 
 - Issue #16408: Fix file descriptors not being closed in error conditions