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)
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()
def test_split_raises_if_not_started(self): sw = StopWatch() with self.assertRaises(RuntimeError): sw.split()