def test_accumulated_explicit_timer(capsys): """Test that explicit timer can accumulate""" t = AccumulatedTimer(TIME_MESSAGE, logger=print) t.start() sum(n**2 for n in range(1000)) t.end() t.start() sum(n**2 for n in range(1000)) t.end() stdout, stderr = capsys.readouterr() lines = stdout.strip().split("\n") assert len(lines) == 2 assert re.match(TIME_MESSAGE + r" 0\.\d{4}/0\.\d{4} seconds", lines[0]) assert re.match(TIME_MESSAGE + r" 0\.\d{4}/0\.\d{4} seconds", lines[1]) assert stderr == ""
def test_resetting_timer(): """Test that timer is reset to 0""" t = AccumulatedTimer(TIME_MESSAGE) t.start() sum(n**2 for n in range(1000)) assert t.end() > 0 t.reset() assert t.end() == 0
def test_consecutive_pauses(): """Test that consecutive pauses just returns 0""" t = AccumulatedTimer(TIME_MESSAGE) t.start() sum(n**2 for n in range(1000)) t.pause() sum(n**2 for n in range(1000)) assert t.pause() == 0
def test_accumulated_context_manager(capsys): """Test that context manager timer can accumulate""" t = AccumulatedTimer(TIME_MESSAGE) with t: sum(n ** 2 for n in range(1000)) with t: sum(n ** 2 for n in range(1000)) stdout, stderr = capsys.readouterr() lines = stdout.strip().split("\n") assert len(lines) == 2 assert re.match(TIME_MESSAGE + r" 0\.\d{4}/0\.\d{4} seconds", lines[0]) assert re.match(TIME_MESSAGE + r" 0\.\d{4}/0\.\d{4} seconds", lines[1]) assert stderr == ""
def test_accumulated_explicit_timer_with_pause(capsys): """Test that explicit timer can be paused""" t = AccumulatedTimer(TIME_MESSAGE, logger=print) laps = list() for _ in range(3): t.start() sum(n**2 for n in range(1000)) laps.append(t.pause()) total = t.end() assert sum(laps) == total stdout, stderr = capsys.readouterr() assert re.match(TIME_MESSAGE + r" 0\.\d{4}/0\.\d{4} seconds", stdout) assert stdout.count("\n") == 1 assert stderr == ""
def test_error_if_resetting_running_timer(): """Test that resetting a running timer raises an error""" t = AccumulatedTimer(TIME_MESSAGE) t.start() with pytest.raises(exceptions.TimerRunning): t.reset()