Skip to content

Commit

Permalink
nose/case.{Function,Method}TestCase: have consistent __repr__
Browse files Browse the repository at this point in the history
Tests were added in the previous commit which showed that modifying a
mutable object given as an argument to a test case during the execution of
the test case results in the __repr__ of the test case also changing (if
the __repr__ of the object also changed). This was caused by creating a
__repr__ of the test args on the fly, and because the summary line is
printed *before* the test is executed and failure details are printed
*after* the test is executed, this can result in the two lines having
different __repr__s of the passed in arguments (see the previous commit
message for an example of the output).

This commit fixes the issue by storing a __repr__ of the argument tuple
when the *TestCase is instantiated and using that to create all __repr__s
of the *TestCase. Note that the `if` for when no args are given had to be
changed because `repr(tuple())` is '()', which is truthy.
  • Loading branch information
LewisHaley committed Oct 16, 2015
1 parent 96b0539 commit 0164f01
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions nose/case.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ def __init__(self, test, setUp=None, tearDown=None, arg=tuple(),
self.setUpFunc = setUp
self.tearDownFunc = tearDown
self.arg = arg
self.arg_repr = repr(self.arg)
self.descriptor = descriptor
TestBase.__init__(self)

Expand Down Expand Up @@ -282,7 +283,7 @@ def __str__(self):
else:
name = func.__name__
name = "%s.%s" % (func.__module__, name)
if arg:
if not arg == '()':
name = "%s%s" % (name, arg)
# FIXME need to include the full dir path to disambiguate
# in cases where test module of the same name was seen in
Expand All @@ -299,9 +300,9 @@ def _descriptors(self):
are returned.
"""
if self.descriptor:
return self.descriptor, self.arg
return self.descriptor, self.arg_repr
else:
return self.test, self.arg
return self.test, self.arg_repr


class MethodTestCase(TestBase):
Expand Down Expand Up @@ -338,6 +339,7 @@ def __init__(self, method, test=None, arg=tuple(), descriptor=None):
self.method = method
self.test = test
self.arg = arg
self.arg_repr = repr(self.arg)
self.descriptor = descriptor
if isfunction(method):
raise ValueError("Unbound methods must be wrapped using pyversion.unbound_method before passing to MethodTestCase")
Expand All @@ -357,7 +359,7 @@ def __str__(self):
name = "%s.%s.%s" % (self.cls.__module__,
self.cls.__name__,
name)
if arg:
if not arg == '()':
name = "%s%s" % (name, arg)
return name
__repr__ = __str__
Expand Down Expand Up @@ -392,6 +394,6 @@ def _descriptors(self):
or function are returned.
"""
if self.descriptor:
return self.descriptor, self.arg
return self.descriptor, self.arg_repr
else:
return self.method, self.arg
return self.method, self.arg_repr

0 comments on commit 0164f01

Please # to comment.