/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

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