~barry/autopilot/lp1488175

« back to all changes in this revision

Viewing changes to autopilot/tests/unit/test_logging.py

  • Committer: CI bot
  • Author(s): Leonardo Arias Fonseca, Thomi Richards, Christopher Lee, Leo Arias, nskaggs
  • Date: 2014-10-31 21:49:56 UTC
  • mfrom: (493.1.26 trunk)
  • Revision ID: ps-jenkins@lists.canonical.com-20141031214956-oaptmny2t0vgj973
Autopilot release:
  - Add support for large timestamps
  - Add per-test timeouts
  - Add press duration to touch clicks
  - Improved logging
  - Make own autopilot functional test runnable with testtools runner.   
Approved by: Nicholas Skaggs, PS Jenkins bot

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
from unittest.mock import Mock
23
23
import testtools
24
24
 
 
25
from autopilot import tests
25
26
from autopilot.logging import log_action
26
27
from autopilot._logging import TestCaseLoggingFixture
27
28
 
28
29
 
29
 
class LogHandlerTestCase(testtools.TestCase):
30
 
    """A mixin that adds a memento loghandler for testing logging.
31
 
 
32
 
    Originally written by:
33
 
     - Guillermo Gonzalez
34
 
     - Facundo Batista
35
 
     - Natalia Bidart
36
 
 
37
 
    """
38
 
 
39
 
    class MementoHandler(logging.Handler):
40
 
        """A handler class which stores logging records in a list."""
41
 
        def __init__(self, *args, **kwargs):
42
 
            """Create the instance, and add a records attribute."""
43
 
            logging.Handler.__init__(self, *args, **kwargs)
44
 
            self.records = []
45
 
 
46
 
        def emit(self, record):
47
 
            """Just add the record to self.records."""
48
 
            self.records.append(record)
49
 
 
50
 
        def check(self, level, msg, check_traceback=False):
51
 
            """Check that something is logged."""
52
 
            result = False
53
 
            for rec in self.records:
54
 
                if rec.levelname == level:
55
 
                    result = str(msg) in rec.getMessage()
56
 
                    if not result and check_traceback:
57
 
                        result = str(msg) in rec.exc_text
58
 
                    if result:
59
 
                        break
60
 
 
61
 
            return result
62
 
 
63
 
    def setUp(self):
64
 
        """Add the memento handler to the root logger."""
65
 
        super(LogHandlerTestCase, self).setUp()
66
 
        self.memento_handler = self.MementoHandler()
67
 
        self.root_logger = logging.getLogger()
68
 
        self.root_logger.addHandler(self.memento_handler)
69
 
 
70
 
    def tearDown(self):
71
 
        """Remove the memento handler from the root logger."""
72
 
        self.root_logger.removeHandler(self.memento_handler)
73
 
        super(LogHandlerTestCase, self).tearDown()
74
 
 
75
 
    def assertLogLevelContains(self, level, message, check_traceback=False):
76
 
        check = self.memento_handler.check(
77
 
            level, message, check_traceback=check_traceback)
78
 
 
79
 
        msg = ('Expected logging message/s could not be found:\n%s\n'
80
 
               'Current logging records are:\n%s')
81
 
        expected = '\t%s: %s' % (level, message)
82
 
        records = ['\t%s: %s' % (r.levelname, r.getMessage())
83
 
                   for r in self.memento_handler.records]
84
 
        self.assertTrue(check, msg % (expected, '\n'.join(records)))
85
 
 
86
 
 
87
30
class ObjectWithLogDecorator(object):
88
31
 
89
32
    @log_action(logging.info)
104
47
        pass
105
48
 
106
49
 
107
 
class LoggingTestCase(LogHandlerTestCase):
 
50
class LoggingTestCase(tests.LogHandlerTestCase):
108
51
 
109
52
    def setUp(self):
110
53
        super(LoggingTestCase, self).setUp()