Kaydet (Commit) 07ef487a authored tarafından Michael Foord's avatar Michael Foord

--no commit message

--no commit message
üst 7430989c
......@@ -1318,6 +1318,20 @@ Loading and running tests
The default implementation does nothing.
.. method:: startTestRun(test)
Called once before any tests are executed.
.. versionadded:: 2.7
.. method:: stopTestRun(test)
Called once before any tests are executed.
.. versionadded:: 2.7
.. method:: addError(test, err)
Called when the test case *test* raises an unexpected exception *err* is a
......
......@@ -517,6 +517,10 @@ changes, or look through the Subversion logs for all the details.
If False ``main`` doesn't call :func:`sys.exit` allowing it to
be used from the interactive interpreter. :issue:`3379`.
:class:`TestResult` has new :meth:`startTestRun` and
:meth:`stopTestRun` methods; called immediately before
and after a test run. :issue:`5728` by Robert Collins.
* The :func:`is_zipfile` function in the :mod:`zipfile` module will now
accept a file object, in addition to the path names accepted in earlier
versions. (Contributed by Gabriel Genellina; :issue:`4756`.)
......
This diff is collapsed.
......@@ -186,10 +186,22 @@ class TestResult(object):
"Called when the given test is about to be run"
self.testsRun = self.testsRun + 1
def startTestRun(self):
"""Called once before any tests are executed.
See startTest for a method called before each test.
"""
def stopTest(self, test):
"Called when the given test has been run"
pass
def stopTestRun(self):
"""Called once after all tests are executed.
See stopTest for a method called after each test.
"""
def addError(self, test, err):
"""Called when an error has occurred. 'err' is a tuple of values as
returned by sys.exc_info().
......@@ -437,8 +449,13 @@ class TestCase(object):
(_strclass(self.__class__), self._testMethodName)
def run(self, result=None):
orig_result = result
if result is None:
result = self.defaultTestResult()
startTestRun = getattr(result, 'startTestRun', None)
if startTestRun is not None:
startTestRun()
self._result = result
result.startTest(self)
testMethod = getattr(self, self._testMethodName)
......@@ -478,6 +495,10 @@ class TestCase(object):
result.addSuccess(self)
finally:
result.stopTest(self)
if orig_result is None:
stopTestRun = getattr(result, 'stopTestRun', None)
if stopTestRun is not None:
stopTestRun()
def doCleanups(self):
"""Execute all cleanup functions. Normally called for you after
......@@ -1433,7 +1454,15 @@ class TextTestRunner(object):
"Run the given test case or test suite."
result = self._makeResult()
startTime = time.time()
test(result)
startTestRun = getattr(result, 'startTestRun', None)
if startTestRun is not None:
startTestRun()
try:
test(result)
finally:
stopTestRun = getattr(result, 'stopTestRun', None)
if stopTestRun is not None:
stopTestRun()
stopTime = time.time()
timeTaken = stopTime - startTime
result.printErrors()
......
......@@ -359,6 +359,17 @@ Library
- unittest.assertNotEqual() now uses the inequality operator (!=) instead
of the equality operator.
- Issue #5679: The methods unittest.TestCase.addCleanup and doCleanups were added.
addCleanup allows you to add cleanup functions that will be called
unconditionally (after setUp if setUp fails, otherwise after tearDown). This
allows for much simpler resource allocation and deallocation during tests.
- Issue #3379: unittest.main now takes an optional exit argument. If False main
doesn't call sys.exit allowing it to be used from the interactive interpreter.
- Issue #5728: unittest.TestResult has new startTestRun and stopTestRun methods;
called immediately before and after a test run.
- Issue #5663: better failure messages for unittest asserts. Default assertTrue
and assertFalse messages are now useful. TestCase has a longMessage attribute.
This defaults to False, but if set to True useful error messages are shown in
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment