Add a test for utils.debug.log_time.

This commit is contained in:
Florian Bruhin 2015-03-16 06:43:56 +01:00
parent d266665955
commit 46c31911a6

View File

@ -19,7 +19,10 @@
"""Tests for qutebrowser.utils.debug.""" """Tests for qutebrowser.utils.debug."""
import re
import time
import unittest import unittest
import logging
from PyQt5.QtCore import Qt from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QStyle, QFrame from PyQt5.QtWidgets import QStyle, QFrame
@ -148,5 +151,21 @@ class TestDebug(unittest.TestCase):
r"fake('foo\nbar')") 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__': if __name__ == '__main__':
unittest.main() unittest.main()