~gmb/zope.testing/make-skips-work-bug-1019275

« back to all changes in this revision

Viewing changes to src/zope/testing/testrunner/formatter.py

  • Committer: Brad Crittenden
  • Date: 2012-06-19 19:49:26 UTC
  • mfrom: (40.1.3 1012171)
  • Revision ID: bac@canonical.com-20120619194926-e599j3y1pfi6hxbp
Tags: p14
[r=benji][bug=1012171] Include data written to stdout or stderr by tests into the subunit output stream.  Bump to p14.

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
import traceback
25
25
 
26
26
from datetime import datetime, timedelta
 
27
from functools import partial
27
28
 
28
29
from zope.testing.exceptions import DocTestFailureException
29
30
 
713
714
    TAG_THREADS = 'zope:threads'
714
715
    TAG_REFCOUNTS = 'zope:refcounts'
715
716
 
 
717
 
716
718
    def __init__(self, options, stream=None):
717
719
        if subunit is None:
718
720
            raise Exception("Requires subunit 0.0.5 or better")
907
909
    def test_success(self, test, seconds):
908
910
        if self._time_tests:
909
911
            self._emit_timestamp()
910
 
        self._subunit.addSuccess(test)
 
912
        details = {}
 
913
        self._add_test_output(details)
 
914
        self._subunit.addSuccess(test, details=details)
911
915
 
912
916
    def import_errors(self, import_errors):
913
917
        """Report test-module import errors (if any)."""
943
947
 
944
948
        return {
945
949
            'traceback': content.Content(
946
 
                self.TRACEBACK_CONTENT_TYPE, lambda: [unicode_tb.encode('utf8')])}
 
950
                self.TRACEBACK_CONTENT_TYPE,
 
951
                lambda: [unicode_tb.encode('utf8')])}
 
952
 
 
953
    def _get_new_stream_output(self, stream):
 
954
        """Get the stream output written since the last time."""
 
955
        try:
 
956
            stream.truncate()
 
957
            msg = stream.getvalue()
 
958
            stream.seek(0)
 
959
        except (AttributeError, IOError):
 
960
            msg = None
 
961
        return msg
 
962
 
 
963
    def _add_test_output(self, details):
 
964
        """If tests write data to stdout or stderr, add it to the details."""
 
965
        msg = self._get_new_stream_output(sys.stdout)
 
966
        if msg:
 
967
            details['STDOUT:'] = content.Content(
 
968
                self.PLAIN_TEXT, partial(lambda x: x, msg))
 
969
        msg = self._get_new_stream_output(sys.stderr)
 
970
        if msg:
 
971
            details['STDERR:'] = content.Content(
 
972
                self.PLAIN_TEXT, partial(lambda x: x, msg))
947
973
 
948
974
    def test_error(self, test, seconds, exc_info):
949
975
        """Report that an error occurred while running a test.
955
981
        if self._time_tests:
956
982
            self._emit_timestamp()
957
983
        details = self._exc_info_to_details(exc_info)
 
984
        self._add_test_output(details)
958
985
        self._subunit.addError(test, details=details)
959
986
 
960
987
    def test_failure(self, test, seconds, exc_info):
967
994
        if self._time_tests:
968
995
            self._emit_timestamp()
969
996
        details = self._exc_info_to_details(exc_info)
 
997
        self._add_test_output(details)
970
998
        self._subunit.addFailure(test, details=details)
971
999
 
972
1000
    def profiler_stats(self, stats):