PageRenderTime 25ms CodeModel.GetById 13ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/unit_tests/test_doctest_munging.rst

https://bitbucket.org/jpellerin/nose/
ReStructuredText | 105 lines | 95 code | 10 blank | 0 comment | 0 complexity | 49776dc6b54890a8cd9c04fe4d5134a2 MD5 | raw file
  1doctest output normalization for plugin testing support
  2=======================================================
  3
  4nose.plugins.plugintest.run() is used for testing nose plugins in
  5doctests, so it needs to normalise nose output to remove information
  6that is not of interest to most plugin tests.
  7
  8We strip stack trace from formatted exceptions, using a regexp copied
  9from ``doctest.py``.  That regexp always matches to the end of a
 10string, so we split on blank lines before running the regexp on each
 11resulting block.
 12
 13    >>> from nose.plugins.plugintest import blankline_separated_blocks
 14    >>> list(blankline_separated_blocks("spam\neggs\n\nfoo\nbar\n\n"))
 15    ['spam\neggs\n\n', 'foo\nbar\n\n']
 16    >>> list(blankline_separated_blocks("spam\neggs\n\nfoo\nbar\n"))
 17    ['spam\neggs\n\n', 'foo\nbar\n']
 18    >>> list(blankline_separated_blocks("spam\neggs\n\nfoo\nbar"))
 19    ['spam\neggs\n\n', 'foo\nbar']
 20    >>> list(blankline_separated_blocks(""))
 21    []
 22    >>> list(blankline_separated_blocks("spam"))
 23    ['spam']
 24
 25``remove_stack_traces`` removes the stack traces, replacing them with
 26an ellipsis.  Note the first line here is chosen not to be "Traceback
 27(most recent...", since doctest would interpret that as meaning that
 28the example should raise an exception!
 29
 30    >>> from nose.plugins.plugintest import remove_stack_traces
 31    >>> print remove_stack_traces("""\
 32    ... Ceci n'est pas une traceback.
 33    ... Traceback (most recent call last):
 34    ...   File "/some/dir/foomodule.py", line 15, in runTest
 35    ...   File "/some/dir/spam.py", line 293, in who_knows_what
 36    ... AssertionError: something bad happened
 37    ... """)
 38    Ceci n'est pas une traceback.
 39    Traceback (most recent call last):
 40    ...
 41    AssertionError: something bad happened
 42    <BLANKLINE>
 43
 44Multiple tracebacks in an example are all replaced, as long as they're
 45separated by blank lines.
 46
 47    >>> print remove_stack_traces("""\
 48    ... Ceci n'est pas une traceback.
 49    ... Traceback (most recent call last):
 50    ...   File spam
 51    ... AttributeError: eggs
 52    ...
 53    ... Traceback (most recent call last):
 54    ...   File eggs
 55    ... AttributeError: spam
 56    ... """)
 57    Ceci n'est pas une traceback.
 58    Traceback (most recent call last):
 59    ...
 60    AttributeError: eggs
 61    <BLANKLINE>
 62    Traceback (most recent call last):
 63    ...
 64    AttributeError: spam
 65    <BLANKLINE>
 66
 67
 68Putting it together, ``munge_nose_output_for_doctest()`` removes stack
 69traces, removes test timings from "Ran n test(s)" output, and strips
 70trailing blank lines.
 71
 72    >>> from nose.plugins.plugintest import munge_nose_output_for_doctest
 73    >>> print munge_nose_output_for_doctest("""\
 74    ... runTest (foomodule.PassingTest) ... ok
 75    ... runTest (foomodule.FailingTest) ... FAIL
 76    ...
 77    ... ======================================================================
 78    ... FAIL: runTest (foomodule.FailingTest)
 79    ... ----------------------------------------------------------------------
 80    ... Traceback (most recent call last):
 81    ...   File "/some/dir/foomodule.py", line 15, in runTest
 82    ...   File "/some/dir/spam.py", line 293, in who_knows_what
 83    ... AssertionError: something bad happened
 84    ...
 85    ... ----------------------------------------------------------------------
 86    ... Ran 1 test in 0.082s
 87    ...
 88    ... FAILED (failures=1)
 89    ...
 90    ...
 91    ... """)
 92    runTest (foomodule.PassingTest) ... ok
 93    runTest (foomodule.FailingTest) ... FAIL
 94    <BLANKLINE>
 95    ======================================================================
 96    FAIL: runTest (foomodule.FailingTest)
 97    ----------------------------------------------------------------------
 98    Traceback (most recent call last):
 99    ...
100    AssertionError: something bad happened
101    <BLANKLINE>
102    ----------------------------------------------------------------------
103    Ran 1 test in ...s
104    <BLANKLINE>
105    FAILED (failures=1)