Exemplo n.º 1
0
class LoggingSuite(TestSuite):

    def __init__(self, tests, name="", level=DEBUG, stdout=sys.stdout):
        super(LoggingSuite, self).__init__()
        self.handler = MemoryHandler(1)
        self._fixture = LogHandler(self.handler, name=name, level=level)
        self._stdout = stdout

        # Euristically figure out if we're being passed a single test/suite
        # or a list of tests. In particular, in case of a single suite we
        # don't want addTests() to unwrap it by iterating through its tests,
        # since that would prevent its run() method from being run and by-pass
        # possible custom logic (e.g. testresources.OptimisingTestSuite).
        if safe_hasattr(tests, "run"):
            add = self.addTest
        else:
            add = self.addTests
        add(tests)

    def run(self, result):
        self.handler.target, result = self._get_target_handler(result)
        self._fixture.setUp()
        try:
            super(LoggingSuite, self).run(result)
        finally:
            self._fixture.cleanUp()

    def _get_target_handler(self, result):
        if isinstance(result, AutoTimingTestResultDecorator):
            target = SubunitHandler()
            result = ExtendedToSubunitHandlerDecorator(result, target)
        else:
            target = StreamHandler(self._stdout)
        return target, result
Exemplo n.º 2
0
    def __init__(self, tests, name="", level=DEBUG, stdout=sys.stdout):
        super(LoggingSuite, self).__init__()
        self.handler = MemoryHandler(1)
        self._fixture = LogHandler(self.handler, name=name, level=level)
        self._stdout = stdout

        # Euristically figure out if we're being passed a single test/suite
        # or a list of tests. In particular, in case of a single suite we
        # don't want addTests() to unwrap it by iterating through its tests,
        # since that would prevent its run() method from being run and by-pass
        # possible custom logic (e.g. testresources.OptimisingTestSuite).
        if safe_hasattr(tests, "run"):
            add = self.addTest
        else:
            add = self.addTests
        add(tests)
Exemplo n.º 3
0
 def setUp(self):
     super(ServiceOutputParserTest, self).setUp()
     self.transport = StringTransport()
     self.handler = BufferingHandler(2)
     self.useFixture(LogHandler(self.handler))
     self.parser = ServiceOutputParser()
     self.parser.setServiceName("my-app")
     self.parser.makeConnection(self.transport)
Exemplo n.º 4
0
 def test_preserving_existing_handlers(self):
     stream = StringIO()
     self.logger.addHandler(logging.StreamHandler(stream))
     self.logger.setLevel(logging.INFO)
     fixture = LogHandler(self.CustomHandler(), nuke_handlers=False)
     with fixture:
         logging.info("message")
     self.assertEqual(["message"], fixture.handler.msgs)
     self.assertEqual("message\n", stream.getvalue())
Exemplo n.º 5
0
 def test_logging_level_restored(self):
     self.logger.setLevel(logging.DEBUG)
     fixture = LogHandler(self.CustomHandler(), level=logging.WARNING)
     with fixture:
         # The fixture won't capture this, because the DEBUG level
         # is lower than the WARNING one
         logging.debug("debug message")
         self.assertEqual(logging.WARNING, self.logger.level)
     self.assertEqual([], fixture.handler.msgs)
     self.assertEqual(logging.DEBUG, self.logger.level)
Exemplo n.º 6
0
 def test_replace_and_restore_handlers(self):
     stream = StringIO()
     logger = logging.getLogger()
     logger.addHandler(logging.StreamHandler(stream))
     logger.setLevel(logging.INFO)
     logging.info("one")
     fixture = LogHandler(self.CustomHandler())
     with fixture:
         logging.info("two")
     logging.info("three")
     self.assertEqual(["two"], fixture.handler.msgs)
     self.assertEqual("one\nthree\n", stream.getvalue())
Exemplo n.º 7
0
 def test_captures_logging(self):
     fixture = self.useFixture(LogHandler(self.CustomHandler()))
     logging.info("some message")
     self.assertEqual(["some message"], fixture.handler.msgs)