• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright (c) 2009, Google Inc. All rights reserved.
2#
3# Redistribution and use in source and binary forms, with or without
4# modification, are permitted provided that the following conditions are
5# met:
6#
7#     * Redistributions of source code must retain the above copyright
8# notice, this list of conditions and the following disclaimer.
9#     * Redistributions in binary form must reproduce the above
10# copyright notice, this list of conditions and the following disclaimer
11# in the documentation and/or other materials provided with the
12# distribution.
13#     * Neither the name of Google Inc. nor the names of its
14# contributors may be used to endorse or promote products derived from
15# this software without specific prior written permission.
16#
17# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28#
29# Class for unittest support.  Used for capturing stderr/stdout.
30
31import sys
32import unittest
33from StringIO import StringIO
34
35class OutputCapture(object):
36    def __init__(self):
37        self.saved_outputs = dict()
38
39    def _capture_output_with_name(self, output_name):
40        self.saved_outputs[output_name] = getattr(sys, output_name)
41        captured_output = StringIO()
42        setattr(sys, output_name, captured_output)
43        return captured_output
44
45    def _restore_output_with_name(self, output_name):
46        captured_output = getattr(sys, output_name).getvalue()
47        setattr(sys, output_name, self.saved_outputs[output_name])
48        del self.saved_outputs[output_name]
49        return captured_output
50
51    def capture_output(self):
52        return (self._capture_output_with_name("stdout"), self._capture_output_with_name("stderr"))
53
54    def restore_output(self):
55        return (self._restore_output_with_name("stdout"), self._restore_output_with_name("stderr"))
56
57    def assert_outputs(self, testcase, function, args=[], kwargs={}, expected_stdout="", expected_stderr="", expected_exception=None):
58        self.capture_output()
59        if expected_exception:
60            return_value = testcase.assertRaises(expected_exception, function, *args, **kwargs)
61        else:
62            return_value = function(*args, **kwargs)
63        (stdout_string, stderr_string) = self.restore_output()
64        testcase.assertEqual(stdout_string, expected_stdout)
65        testcase.assertEqual(stderr_string, expected_stderr)
66        # This is a little strange, but I don't know where else to return this information.
67        return return_value
68
69
70class OutputCaptureTestCaseBase(unittest.TestCase):
71    def setUp(self):
72        unittest.TestCase.setUp(self)
73        self.output_capture = OutputCapture()
74        (self.__captured_stdout, self.__captured_stderr) = self.output_capture.capture_output()
75
76    def tearDown(self):
77        del self.__captured_stdout
78        del self.__captured_stderr
79        self.output_capture.restore_output()
80        unittest.TestCase.tearDown(self)
81
82    def assertStdout(self, expected_stdout):
83        self.assertEquals(expected_stdout, self.__captured_stdout.getvalue())
84
85    def assertStderr(self, expected_stderr):
86        self.assertEquals(expected_stderr, self.__captured_stderr.getvalue())
87