diff --git a/qutebrowser/test/utils/test_debug.py b/qutebrowser/test/utils/test_debug.py index 926f42261..12a5afef1 100644 --- a/qutebrowser/test/utils/test_debug.py +++ b/qutebrowser/test/utils/test_debug.py @@ -19,7 +19,10 @@ """Tests for qutebrowser.utils.debug.""" +import re +import time import unittest +import logging from PyQt5.QtCore import Qt from PyQt5.QtWidgets import QStyle, QFrame @@ -148,5 +151,21 @@ class TestDebug(unittest.TestCase): r"fake('foo\nbar')") +class TestLogTime(unittest.TestCase): + + """Test log_time.""" + + def test_log_time(self): + logger = logging.getLogger('qt-tests') + with self.assertLogs(logger, logging.DEBUG) as logged: + with debug.log_time(logger, action='foobar'): + time.sleep(0.1) + self.assertEqual(len(logged.records), 1) + pattern = re.compile(r'^Foobar took ([\d.]*) seconds\.$') + match = pattern.match(logged.records[0].msg) + self.assertTrue(match) + duration = float(match.group(1)) + self.assertAlmostEqual(duration, 0.1, delta=0.01) + if __name__ == '__main__': unittest.main()