From 0164f01e4804940b92459ac7f52eb5d021506be6 Mon Sep 17 00:00:00 2001 From: Lewis Haley Date: Thu, 24 Sep 2015 12:43:08 +0100 Subject: [PATCH] nose/case.{Function,Method}TestCase: have consistent __repr__ 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. --- nose/case.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/nose/case.py b/nose/case.py index cffa4ab4..e5a407a0 100644 --- a/nose/case.py +++ b/nose/case.py @@ -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) @@ -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 @@ -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): @@ -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") @@ -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__ @@ -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