From ca8349a897c233d72ea74128dabdd1311f00c13c Mon Sep 17 00:00:00 2001 From: "Paul \"LeoNerd\" Evans" Date: Fri, 12 Sep 2014 18:29:07 +0100 Subject: [PATCH] Allow a TestCase to set a 'loglevel' attribute, which overrides the logging level while that testcase runs --- tests/unittest.py | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/tests/unittest.py b/tests/unittest.py index 00c3c532e..19be03b96 100644 --- a/tests/unittest.py +++ b/tests/unittest.py @@ -27,4 +27,25 @@ logging.getLogger().setLevel(NEVER) class TestCase(unittest.TestCase): - pass + def __init__(self, *args, **kwargs): + super(TestCase, self).__init__(*args, **kwargs) + + level = getattr(self, "loglevel", NEVER) + + orig_setUp = self.setUp + + def setUp(): + old_level = logging.getLogger().level + + if old_level != level: + orig_tearDown = self.tearDown + + def tearDown(): + ret = orig_tearDown() + logging.getLogger().setLevel(old_level) + return ret + self.tearDown = tearDown + + logging.getLogger().setLevel(level) + return orig_setUp() + self.setUp = setUp