Skip to content

Commit

Permalink
be as helpful as we can in giving a hint for a failing test against t…
Browse files Browse the repository at this point in the history
…raceback output.
  • Loading branch information
janwijbrand committed Sep 29, 2016
1 parent 62b7fa0 commit 2f1882a
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 4 deletions.
30 changes: 26 additions & 4 deletions src/zope/testing/renormalizing.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@


EXCEPTION_2TO3 = doctest.register_optionflag('EXCEPTION_2TO3')
EXCEPTION_2TO3_HINT = """
===============================================================
HINT:
You seem to test traceback output.
The optionflag EXCEPTION_2TO3 is set.
Do you use the full dotted name for the exception class name?
==============================================================="""


class OutputChecker(doctest.OutputChecker):
Expand Down Expand Up @@ -71,6 +78,12 @@ def output_difference(self, example, got, optionflags):

# temporarily hack example with normalized want:
example.want = want

if sys.version < '3':
if optionflags & EXCEPTION_2TO3:
if maybe_a_traceback(got) is not None:
got += EXCEPTION_2TO3_HINT

result = doctest.OutputChecker.output_difference(
self, example, got, optionflags)
example.want = original
Expand All @@ -80,19 +93,28 @@ def output_difference(self, example, got, optionflags):
RENormalizing = OutputChecker


def strip_dottedname_from_traceback(string):
# We might want to confirm more strictly were dealing with a traceback.
# We'll assume so for now.
def maybe_a_traceback(string):
if not string:
return string
return None

lines = string.splitlines()
last = lines[-1]
words = last.split(' ')
first = words[0]
if not first.endswith(':'):
return None

return lines, last, words, first


def strip_dottedname_from_traceback(string):
# We might want to confirm more strictly were dealing with a traceback.
# We'll assume so for now.
maybe = maybe_a_traceback(string)
if maybe is None:
return string

lines, last, words, first = maybe
name = first.split('.')[-1]
words[0] = name
last = ' '.join(words)
Expand Down
35 changes: 35 additions & 0 deletions src/zope/testing/renormalizing.txt
Original file line number Diff line number Diff line change
Expand Up @@ -252,3 +252,38 @@ Using the 2to3 exception normalization:
... expected = False
>>> result == expected
True

When reporting a failing test and running in Python 2, the normalizer tries
to be helpful by explaining how to test for exceptions in the traceback output.

>>> want = """\
... Traceback (most recent call last):
... foo.bar.FooBarErrorXX: requires at least one argument."""
>>> got = """\
... Traceback (most recent call last):
... FooBarError: requires at least one argument."""
>>> checker.check_output(want, got, EXCEPTION_2TO3)
False
>>> from doctest import Example
>>> example = Example('dummy', want)
>>> result = checker.output_difference(example, got, EXCEPTION_2TO3)
>>> output = """\
... Expected:
... Traceback (most recent call last):
... foo.bar.FooBarErrorXX: requires at least one argument.
... Got:
... Traceback (most recent call last):
... FooBarError: requires at least one argument."""
>>> hint = """
... ===============================================================
... HINT:
... You seem to test traceback output.
... The optionflag EXCEPTION_2TO3 is set.
... Do you use the full dotted name for the exception class name?
... ==============================================================="""
>>> if sys.version < '3':
... expected = output + hint
... else:
... expected = output
>>> result == expected
True

0 comments on commit 2f1882a

Please sign in to comment.