From 056a1a8ea429d2caf6acc58e3575900ffda0a984 Mon Sep 17 00:00:00 2001 From: Richard Belleville Date: Thu, 1 Nov 2018 13:24:03 -0700 Subject: [PATCH] Isolate logging in all test cases --- .../grpcio_tests/tests/unit/_logging_test.py | 40 +++++++++++-------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/src/python/grpcio_tests/tests/unit/_logging_test.py b/src/python/grpcio_tests/tests/unit/_logging_test.py index cc814ba46b9..d1384effde9 100644 --- a/src/python/grpcio_tests/tests/unit/_logging_test.py +++ b/src/python/grpcio_tests/tests/unit/_logging_test.py @@ -36,32 +36,38 @@ def patch_stderr(f): return _impl +def isolated_logging(f): + + @functools.wraps(f) + def _impl(*args, **kwargs): + reload_module(logging) + reload_module(grpc) + try: + f(*args, **kwargs) + finally: + reload_module(logging) + + return _impl + + class LoggingTest(unittest.TestCase): + @isolated_logging def test_logger_not_occupied(self): self.assertEqual(0, len(logging.getLogger().handlers)) @patch_stderr + @isolated_logging def test_handler_found(self): - try: - reload_module(logging) - reload_module(grpc) - self.assertFalse( - "No handlers could be found" in sys.stderr.getvalue()) - finally: - reload_module(logging) + self.assertEqual(0, len(sys.stderr.getvalue())) + @isolated_logging def test_can_configure_logger(self): - reload_module(logging) - reload_module(grpc) - try: - intended_stream = six.StringIO() - logging.basicConfig(stream=intended_stream) - self.assertEqual(1, len(logging.getLogger().handlers)) - self.assertTrue( - logging.getLogger().handlers[0].stream is intended_stream) - finally: - reload_module(logging) + intended_stream = six.StringIO() + logging.basicConfig(stream=intended_stream) + self.assertEqual(1, len(logging.getLogger().handlers)) + self.assertTrue( + logging.getLogger().handlers[0].stream is intended_stream) if __name__ == '__main__':