Factor out the bit that gets the test method.

This commit is contained in:
Jonathan Lange
2010-10-17 16:46:01 +01:00
parent 859a79aa84
commit 84482c3ebc
2 changed files with 12 additions and 10 deletions

View File

@@ -476,20 +476,22 @@ class TestCase(unittest.TestCase):
"super(%s, self).tearDown() from your tearDown()."
% self.__class__.__name__)
def _run_test_method(self, result):
"""Run the test method for this test.
:param result: A testtools.TestResult to report activity to.
:return: None.
"""
def _get_test_method(self):
absent_attr = object()
# Python 2.5+
method_name = getattr(self, '_testMethodName', absent_attr)
if method_name is absent_attr:
# Python 2.4
method_name = getattr(self, '_TestCase__testMethodName')
testMethod = getattr(self, method_name)
testMethod()
return getattr(self, method_name)
def _run_test_method(self, result):
"""Run the test method for this test.
:param result: A testtools.TestResult to report activity to.
:return: None.
"""
self._get_test_method()()
def setUp(self):
unittest.TestCase.setUp(self)

View File

@@ -406,7 +406,7 @@ Traceback (most recent call last):
File "...testtools...runtest.py", line ..., in _run_user...
return fn(*args)
File "...testtools...testcase.py", line ..., in _run_test_method
testMethod()
self._get_test_method()()
File "...testtools...tests...test_testresult.py", line ..., in error
1/0
ZeroDivisionError:... divi... by zero...
@@ -420,7 +420,7 @@ Traceback (most recent call last):
File "...testtools...runtest.py", line ..., in _run_user...
return fn(*args)
File "...testtools...testcase.py", line ..., in _run_test_method
testMethod()
self._get_test_method()()
File "...testtools...tests...test_testresult.py", line ..., in failed
self.fail("yo!")
AssertionError: yo!