def test_split(self): sw = StopWatch() sw.start() self.assertEqual(0, sw.splits_count) ### split 1 delay1 = 0.5 time.sleep(delay1) sw.split() self.assertEqual(1, sw.splits_count) self.assertAlmostEqual(delay1, sw.last_split_time.total_seconds(), places=1) self.assertAlmostEqual(delay1, sw.mean_split_time.total_seconds(), places=1) self.assertEqual(sw.last_split_time, sw.total_split_time) ### split 1 delay2 = 1.0 time.sleep(delay2) sw.split() self.assertEqual(2, sw.splits_count) self.assertAlmostEqual(delay2, sw.last_split_time.total_seconds(), places=1) self.assertAlmostEqual((delay1 + delay2) / 2, sw.mean_split_time.total_seconds(), places=1) self.assertAlmostEqual(delay1 + delay2, sw.total_split_time.total_seconds(), places=1)
def test_is_started(self): sw = StopWatch() self.assertFalse(sw.is_started) sw.start() self.assertTrue(sw.is_started) sw.reset() self.assertFalse(sw.is_started)
class TestExecStopwatch(BaseResult): """Time test execution using a stopwatch. Use it as a mix-in of a unittest's result class. """ def __init__(self, **kwds): super().__init__(**kwds) self.stopwatch = StopWatch() def startTest(self, test): if not self.stopwatch.is_started: self.stopwatch.start() super().startTest(test) def stopTest(self, test): super().stopTest(test) self.stopwatch.split()
class Walltime(BaseResult): """Compute the whole execution time of the test suite. This is not the sum of the execution time of each test. It is useful to measure the speedup of running test in parallel. Use it as a mix-in of a unittest's result class. """ def __init__(self, **kwds): super().__init__(**kwds) self._walltime_watch = StopWatch() @property def walltime(self): return self._walltime_watch.total_time def startTest(self, test): if not self._walltime_watch.is_started: self._walltime_watch.start() super().startTest(test)
def test_start_raises_if_already_started(self): sw = StopWatch() sw.start() with self.assertRaises(RuntimeError): sw.start()
def test_total_time(self): sw = StopWatch() sw.start() delay = 0.5 time.sleep(delay) self.assertAlmostEqual(delay, sw.total_time.total_seconds(), places=1)