Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
subunit - save arbitrary test result details to files
#!/usr/bin/env python3
import logging
import re
from subunit import ByteStreamToStreamResult
from testtools import StreamToExtendedDecorator, TestResult
logging.basicConfig(level='INFO')
logger = logging.getLogger(__name__)
class FileSaveResult(TestResult):
"""A TestResult that saves arbitrary test details to files."""
def addError(self, test, err=None, details=None):
super().addError(test, err=err, details=details)
self.save_file_attachments(test.id(), details)
def addExpectedFailure(self, test, err=None, details=None):
super().addExpectedFailure(test, err=err, details=details)
self.save_file_attachments(test.id(), details)
def addFailure(self, test, err=None, details=None):
super().addFailure(test, err=err, details=details)
self.save_file_attachments(test.id(), details)
def addSkip(self, test, reason=None, details=None):
super().addSkip(test, reason=reason, details=details)
self.save_file_attachments(test.id(), details)
def addSuccess(self, test, details=None):
super().addSuccess(test, details=details)
self.save_file_attachments(test.id(), details)
def addUnexpectedSuccess(self, test, details=None):
super().addUnexpectedSuccess(test, details=details)
self.save_file_attachments(test.id(), details)
def save_file_attachments(self, test_id, details):
for key, detail in details.items():
if detail.iter_bytes() != [b'']:
slug = re.sub(r'(?u)[^-\w.]', '', key)
file_name = '%s-%s' % (test_id, slug)
logger.info('saved detail from test: %r' % test_id)
logger.info(' attachment: %r (%s)' % (key,
detail.content_type))
logger.info(' saved as: %r' % file_name)
with open(file_name, 'wb') as f:
for chunk in detail.iter_bytes():
f.write(chunk)
def main():
subunit_file = 'results.sub'
stream = open(subunit_file, 'rb')
suite = ByteStreamToStreamResult(stream)
result = StreamToExtendedDecorator(FileSaveResult())
result.startTestRun()
try:
suite.run(result)
finally:
result.stopTestRun()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.