Merge "Different approach to indicate failure on SystemExit"

This commit is contained in:
Jenkins
2015-07-16 06:57:16 +00:00
committed by Gerrit Code Review
2 changed files with 28 additions and 21 deletions

View File

@@ -22,7 +22,6 @@ import logging as std_logging
import os import os
import os.path import os.path
import random import random
import traceback
import weakref import weakref
import eventlet.timeout import eventlet.timeout
@@ -171,8 +170,8 @@ class DietTestCase(testtools.TestCase):
if os.getpid() != self.orig_pid: if os.getpid() != self.orig_pid:
# Subprocess - let it just exit # Subprocess - let it just exit
raise raise
self.fail("A SystemExit was raised during the test. %s" # This makes sys.exit(0) still a failure
% traceback.format_exception(*exc_info)) self.force_failure = True
@contextlib.contextmanager @contextlib.contextmanager
def assert_max_execution_time(self, max_execution_time=5): def assert_max_execution_time(self, max_execution_time=5):

View File

@@ -16,28 +16,36 @@
"""Tests to test the test framework""" """Tests to test the test framework"""
import sys import sys
import unittest2
from neutron.tests import base from neutron.tests import base
class SystemExitTestCase(base.BaseTestCase): class SystemExitTestCase(base.DietTestCase):
# Embedded to hide from the regular test discovery
class MyTestCase(base.DietTestCase):
def __init__(self, exitcode):
super(SystemExitTestCase.MyTestCase, self).__init__()
self.exitcode = exitcode
def setUp(self): def runTest(self):
def _fail_SystemExit(exc_info): if self.exitcode is not None:
if isinstance(exc_info[1], SystemExit): sys.exit(self.exitcode)
self.fail("A SystemExit was allowed out")
super(SystemExitTestCase, self).setUp()
# add the handler last so reaching it means the handler in BaseTestCase
# didn't do it's job
self.addOnException(_fail_SystemExit)
def run(self, *args, **kwargs): def test_no_sysexit(self):
exc = self.assertRaises(AssertionError, result = self.MyTestCase(exitcode=None).run()
super(SystemExitTestCase, self).run, self.assertTrue(result.wasSuccessful())
*args, **kwargs)
# this message should be generated when SystemExit is raised by a test
self.assertIn('A SystemExit was raised during the test.', str(exc))
def test_system_exit(self): def test_sysexit(self):
# this should generate a failure that mentions SystemExit was used expectedFails = [self.MyTestCase(exitcode) for exitcode in (0, 1)]
sys.exit(1)
suite = unittest2.TestSuite(tests=expectedFails)
result = self.defaultTestResult()
try:
suite.run(result)
except SystemExit:
self.fail('SystemExit escaped!')
self.assertEqual([], result.errors)
self.assertItemsEqual(set(id(t) for t in expectedFails),
set(id(t) for (t, traceback) in result.failures))