From e4b4f3131b4e66ca194aabc5e300e49c13f8122e Mon Sep 17 00:00:00 2001 From: Richard Belleville Date: Wed, 31 Oct 2018 16:45:54 -0700 Subject: [PATCH] Pull out function to patch stderr --- .../grpcio_tests/tests/unit/_logging_test.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/python/grpcio_tests/tests/unit/_logging_test.py b/src/python/grpcio_tests/tests/unit/_logging_test.py index 662cd1ea9dc..39c3afbfc84 100644 --- a/src/python/grpcio_tests/tests/unit/_logging_test.py +++ b/src/python/grpcio_tests/tests/unit/_logging_test.py @@ -21,21 +21,30 @@ import grpc import functools import sys +def patch_stderr(f): + @functools.wraps(f) + def _impl(*args, **kwargs): + old_stderr = sys.stderr + sys.stderr = six.StringIO() + try: + f(args, kwargs) + finally: + sys.stderr = old_stderr + return _impl + class LoggingTest(unittest.TestCase): def test_logger_not_occupied(self): self.assertEqual(0, len(logging.getLogger().handlers)) + @patch_stderr def test_handler_found(self): - old_stderr = sys.stderr - sys.stderr = six.StringIO() try: reload_module(logging) logging.basicConfig() reload_module(grpc) self.assertFalse("No handlers could be found" in sys.stderr.getvalue()) finally: - sys.stderr = old_stderr reload_module(logging) if __name__ == '__main__':