From a23cc737982df35ed76635d1a23d6ff040bfd355 Mon Sep 17 00:00:00 2001 From: Ruby Loo Date: Thu, 12 Feb 2015 16:55:04 +0000 Subject: [PATCH] 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 --- .testr.conf | 2 +- ironicclient/tests/utils.py | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/.testr.conf b/.testr.conf index 36d60093d..3ab1aaf93 100644 --- a/.testr.conf +++ b/.testr.conf @@ -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 diff --git a/ironicclient/tests/utils.py b/ironicclient/tests/utils.py index 3a109e498..2ac90fd01 100644 --- a/ironicclient/tests/utils.py +++ b/ironicclient/tests/utils.py @@ -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):