From 91964b910a29b9793d9298a6d1b236341ba90e9e Mon Sep 17 00:00:00 2001 From: Federico Ressi Date: Thu, 26 Sep 2019 17:24:37 +0200 Subject: [PATCH] Write to log the begin and the end of every test case. Change-Id: Id5b07b30be076929cb8d82b5892a0ea71ce57c7f --- tobiko/common/_logging.py | 10 +++++++--- tobiko/tests/unit/test_logging.py | 6 +++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/tobiko/common/_logging.py b/tobiko/common/_logging.py index 2ecbfd767..a4cb77e4c 100644 --- a/tobiko/common/_logging.py +++ b/tobiko/common/_logging.py @@ -33,8 +33,9 @@ class CaptureLogFixture(_fixture.SharedFixture): logger = logging.root handler = None - def __init__(self, logger=None, level=None): + def __init__(self, test_case_id, logger=None, level=None): super(CaptureLogFixture, self).__init__() + self.test_case_id = test_case_id if logger: self.logger = logger if level: @@ -42,8 +43,10 @@ class CaptureLogFixture(_fixture.SharedFixture): def setup_fixture(self): self.handler = handler = CaptureLogHandler(level=self.level) - self.logger.addHandler(handler) self.addCleanup(self.logger.removeHandler, handler) + self.logger.addHandler(handler) + LOG.debug('--- BEGIN %s ---', self.test_case_id) + self.addCleanup(LOG.debug, '--- END %s ---', self.test_case_id) def getDetails(self): handler = self.handler @@ -84,6 +87,7 @@ class CaptureLogTest(testtools.TestCase): capture_log_logger = logging.root def setUp(self): - self.useFixture(CaptureLogFixture(level=self.capture_log_level, + self.useFixture(CaptureLogFixture(test_case_id=self.id(), + level=self.capture_log_level, logger=self.capture_log_logger)) super(CaptureLogTest, self).setUp() diff --git a/tobiko/tests/unit/test_logging.py b/tobiko/tests/unit/test_logging.py index 4a4a4aa22..7bce01704 100644 --- a/tobiko/tests/unit/test_logging.py +++ b/tobiko/tests/unit/test_logging.py @@ -26,7 +26,7 @@ LOG = log.getLogger(__name__) class CapureLogTest(unit.TobikoUnitTest): def test_capture_log_content(self): - with tobiko.CaptureLogFixture() as capture: + with tobiko.CaptureLogFixture(test_case_id=self.id()) as capture: LOG.warning('Some debug line') - content = capture.getDetails()['log'] - self.assertEqual('Some debug line\n', content.as_text()) + logged_lines = capture.getDetails()['log'].as_text().splitlines() + self.assertIn('Some debug line', logged_lines)