Capture stdout or stderr in tests

This uses the environment variables OS_STDOUT_CAPTURE and
OS_STDERR_CAPTURE to determine whether or not to capture
stdout and stderr (respectively) in the unit tests. These
will appear in the test results for tests that fail.

Capturing will be done if the value is set to one of
(case-insensitive) '1', 't', 'true', 'on', 'y', 'yes'.
Any other value will turn off capturing. The default (via
.testr.conf) is to capture.

Change-Id: I3e6006aa1ed1cf3fad7e55a7201be1c10d165431
Closes-Bug: #1421307
This commit is contained in:
Ruby Loo
2015-02-12 16:55:04 +00:00
parent 4390a21663
commit a23cc73798
2 changed files with 12 additions and 1 deletions

View File

@@ -1,5 +1,5 @@
[DEFAULT]
test_command=OS_STDOUT_CAPTURE=1 OS_STDERR_CAPTURE=1 OS_TEST_TIMEOUT=60 ${PYTHON:-python} -m subunit.run discover -t ./ ./ $LISTOPT $IDOPTION
test_command=OS_STDOUT_CAPTURE=${OS_STDOUT_CAPTURE:-1} OS_STDERR_CAPTURE=${OS_STDERR_CAPTURE:-1} OS_TEST_TIMEOUT=60 ${PYTHON:-python} -m subunit.run discover -t ./ ./ $LISTOPT $IDOPTION
test_id_option=--load-list $IDFILE
test_list_option=--list

View File

@@ -17,8 +17,10 @@
import copy
import datetime
import os
import fixtures
from oslo_utils import strutils
import six
import testtools
@@ -31,6 +33,15 @@ class BaseTestCase(testtools.TestCase):
super(BaseTestCase, self).setUp()
self.useFixture(fixtures.FakeLogger())
# If enabled, stdout and/or stderr is captured and will appear in
# test results if that test fails.
if strutils.bool_from_string(os.environ.get('OS_STDOUT_CAPTURE')):
stdout = self.useFixture(fixtures.StringStream('stdout')).stream
self.useFixture(fixtures.MonkeyPatch('sys.stdout', stdout))
if strutils.bool_from_string(os.environ.get('OS_STDERR_CAPTURE')):
stderr = self.useFixture(fixtures.StringStream('stderr')).stream
self.useFixture(fixtures.MonkeyPatch('sys.stderr', stderr))
class FakeAPI(object):
def __init__(self, responses):