summaryrefslogtreecommitdiff
path: root/python/subunit/test_results.py
diff options
context:
space:
mode:
authorRobert Collins <robertc@robertcollins.net>2009-12-11 13:41:42 +1100
committerRobert Collins <robertc@robertcollins.net>2009-12-11 13:41:42 +1100
commit0fdc43947a17af18013bb101a101d3b709ffebc1 (patch)
treeb66b431f0f2e599a798e775fd73bbfa51317629d /python/subunit/test_results.py
parentf0065793fdce9b3c25d40ce8e3a4c112f21d371d (diff)
downloadsubunit-git-0fdc43947a17af18013bb101a101d3b709ffebc1.tar.gz
Use testtools facilities for the details API.
Diffstat (limited to 'python/subunit/test_results.py')
-rw-r--r--python/subunit/test_results.py160
1 files changed, 2 insertions, 158 deletions
diff --git a/python/subunit/test_results.py b/python/subunit/test_results.py
index 8bfdce5..4ccc2aa 100644
--- a/python/subunit/test_results.py
+++ b/python/subunit/test_results.py
@@ -19,6 +19,7 @@
import datetime
import iso8601
+import testtools
import subunit
@@ -37,7 +38,7 @@ class TestResultDecorator(object):
def __init__(self, decorated):
"""Create a TestResultDecorator forwarding to decorated."""
# Make every decorator degrade gracefully.
- self.decorated = ExtendedToOriginalDecorator(decorated)
+ self.decorated = testtools.ExtendedToOriginalDecorator(decorated)
def startTest(self, test):
return self.decorated.startTest(test)
@@ -331,160 +332,3 @@ class TestResultFilter(TestResultDecorator):
if id.startswith("subunit.RemotedTestCase."):
return id[len("subunit.RemotedTestCase."):]
return id
-
-
-class ExtendedToOriginalDecorator(object):
- """Permit new TestResult API code to degrade gracefully with old results.
-
- This decorates an existing TestResult and converts missing outcomes
- such as addSkip to older outcomes such as addSuccess. It also supports
- the extended details protocol. In all cases the most recent protocol
- is attempted first, and fallbacks only occur when the decorated result
- does not support the newer style of calling.
- """
-
- def __init__(self, decorated):
- self.decorated = decorated
-
- def addError(self, test, err=None, details=None):
- self._check_args(err, details)
- if details is not None:
- try:
- return self.decorated.addError(test, details=details)
- except TypeError, e:
- # have to convert
- err = self._details_to_exc_info(details)
- return self.decorated.addError(test, err)
-
- def addExpectedFailure(self, test, err=None, details=None):
- self._check_args(err, details)
- addExpectedFailure = getattr(self.decorated, 'addExpectedFailure', None)
- if addExpectedFailure is None:
- return self.addSuccess(test)
- if details is not None:
- try:
- return addExpectedFailure(test, details=details)
- except TypeError, e:
- # have to convert
- err = self._details_to_exc_info(details)
- return addExpectedFailure(test, err)
-
- def addFailure(self, test, err=None, details=None):
- self._check_args(err, details)
- if details is not None:
- try:
- return self.decorated.addFailure(test, details=details)
- except TypeError, e:
- # have to convert
- err = self._details_to_exc_info(details)
- return self.decorated.addFailure(test, err)
-
- def addSkip(self, test, reason=None, details=None):
- self._check_args(reason, details)
- addSkip = getattr(self.decorated, 'addSkip', None)
- if addSkip is None:
- return self.decorated.addSuccess(test)
- if details is not None:
- try:
- return addSkip(test, details=details)
- except TypeError, e:
- # have to convert
- reason = self._details_to_str(details)
- return addSkip(test, reason)
-
- def addUnexpectedSuccess(self, test, details=None):
- outcome = getattr(self.decorated, 'addUnexpectedSuccess', None)
- if outcome is None:
- return self.decorated.addSuccess(test)
- if details is not None:
- try:
- return outcome(test, details=details)
- except TypeError, e:
- pass
- return outcome(test)
-
- def addSuccess(self, test, details=None):
- if details is not None:
- try:
- return self.decorated.addSuccess(test, details=details)
- except TypeError, e:
- pass
- return self.decorated.addSuccess(test)
-
- def _check_args(self, err, details):
- param_count = 0
- if err is not None:
- param_count += 1
- if details is not None:
- param_count += 1
- if param_count != 1:
- raise ValueError("Must pass only one of err '%s' and details '%s"
- % (err, details))
-
- def _details_to_exc_info(self, details):
- """Convert a details dict to an exc_info tuple."""
- return subunit.RemoteError(self._details_to_str(details))
-
- def _details_to_str(self, details):
- """Convert a details dict to a string."""
- lines = []
- # sorted is for testing, may want to remove that and use a dict
- # subclass with defined order for iteritems instead.
- for key, content in sorted(details.iteritems()):
- if content.content_type.type != 'text':
- lines.append('Binary content: %s\n' % key)
- continue
- lines.append('Text attachment: %s\n' % key)
- lines.append('------------\n')
- lines.extend(content.iter_bytes())
- if not lines[-1].endswith('\n'):
- lines.append('\n')
- lines.append('------------\n')
- return ''.join(lines)
-
- def progress(self, offset, whence):
- method = getattr(self.decorated, 'progress', None)
- if method is None:
- return
- return method(offset, whence)
-
- @property
- def shouldStop(self):
- return self.decorated.shouldStop
-
- def startTest(self, test):
- return self.decorated.startTest(test)
-
- def startTestRun(self):
- try:
- return self.decorated.startTestRun()
- except AttributeError:
- return
-
- def stop(self):
- return self.decorated.stop()
-
- def stopTest(self, test):
- return self.decorated.stopTest(test)
-
- def stopTestRun(self):
- try:
- return self.decorated.stopTestRun()
- except AttributeError:
- return
-
- def tags(self, new_tags, gone_tags):
- method = getattr(self.decorated, 'tags', None)
- if method is None:
- return
- return method(new_tags, gone_tags)
-
- def time(self, a_datetime):
- method = getattr(self.decorated, 'time', None)
- if method is None:
- return
- return method(a_datetime)
-
- def wasSuccessful(self):
- return self.decorated.wasSuccessful()
-