From a034aa1f9546bf13f0f6f90db78da18410b4ae6c Mon Sep 17 00:00:00 2001 From: Joe Gordon Date: Fri, 31 May 2013 16:06:37 -0700 Subject: [PATCH] Fix tests to redirect stdout Add default TestCase that uses fixtures to save stdout and stderr (based off nova/test.py). save stdout and stderr by default but overrideable in order to run pdb. Also use fixtures.Timeout Change-Id: Iece6a96e5e54e252df943934ea465db78e1c5421 --- .testr.conf | 6 +++++- hacking/tests/__init__.py | 30 ++++++++++++++++++++++++++++++ hacking/tests/test_doctest.py | 4 ++-- 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/.testr.conf b/.testr.conf index 1641f86e..748029b4 100644 --- a/.testr.conf +++ b/.testr.conf @@ -1,4 +1,8 @@ [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=${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/hacking/tests/__init__.py b/hacking/tests/__init__.py index f745a135..b2d3df32 100644 --- a/hacking/tests/__init__.py +++ b/hacking/tests/__init__.py @@ -12,3 +12,33 @@ # implied. # See the License for the specific language governing permissions and # limitations under the License. + +import os + +import fixtures +import testtools + + +class TestCase(testtools.TestCase): + """Test case base class for all unit tests.""" + + def setUp(self): + """Run before each test method to initialize test environment.""" + super(TestCase, self).setUp() + test_timeout = os.environ.get('OS_TEST_TIMEOUT', 0) + try: + test_timeout = int(test_timeout) + except ValueError: + # If timeout value is invalid do not set a timeout. + test_timeout = 0 + if test_timeout > 0: + self.useFixture(fixtures.Timeout(test_timeout, gentle=True)) + + if (os.environ.get('OS_STDOUT_CAPTURE') == 'True' or + os.environ.get('OS_STDOUT_CAPTURE') == '1'): + stdout = self.useFixture(fixtures.StringStream('stdout')).stream + self.useFixture(fixtures.MonkeyPatch('sys.stdout', stdout)) + if (os.environ.get('OS_STDERR_CAPTURE') == 'True' or + os.environ.get('OS_STDERR_CAPTURE') == '1'): + stderr = self.useFixture(fixtures.StringStream('stderr')).stream + self.useFixture(fixtures.MonkeyPatch('sys.stderr', stderr)) diff --git a/hacking/tests/test_doctest.py b/hacking/tests/test_doctest.py index a9f7c3f3..ea2c21e1 100644 --- a/hacking/tests/test_doctest.py +++ b/hacking/tests/test_doctest.py @@ -18,20 +18,20 @@ import re from flake8 import engine import pep8 -import testtools from testtools import content from testtools import matchers import testscenarios import hacking +import hacking.tests SELFTEST_REGEX = re.compile(r'\b(Okay|[HEW]\d{3}):\s(.*)') # Each scenario is (name, dict(lines=.., options=..., code=...)) file_cases = [] -class HackingTestCase(testtools.TestCase): +class HackingTestCase(hacking.tests.TestCase): scenarios = file_cases