Mocks _pytest.timing with a known object that can be used to control timing in tests deterministically. pytest itself should always use functions from `_pytest.timing` instead of `time` directly. This then allows us more control over time during testing, if testing code also uses `
| 66 | |
| 67 | @dataclasses.dataclass |
| 68 | class MockTiming: |
| 69 | """Mocks _pytest.timing with a known object that can be used to control timing in tests |
| 70 | deterministically. |
| 71 | |
| 72 | pytest itself should always use functions from `_pytest.timing` instead of `time` directly. |
| 73 | |
| 74 | This then allows us more control over time during testing, if testing code also |
| 75 | uses `_pytest.timing` functions. |
| 76 | |
| 77 | Time is static, and only advances through `sleep` calls, thus tests might sleep over large |
| 78 | numbers and obtain accurate time() calls at the end, making tests reliable and instant.""" |
| 79 | |
| 80 | _current_time: float = datetime(2020, 5, 22, 14, 20, 50).timestamp() |
| 81 | |
| 82 | def sleep(self, seconds: float) -> None: |
| 83 | self._current_time += seconds |
| 84 | |
| 85 | def time(self) -> float: |
| 86 | return self._current_time |
| 87 | |
| 88 | def patch(self, monkeypatch: MonkeyPatch) -> None: |
| 89 | # pylint: disable-next=import-self |
| 90 | from _pytest import timing # noqa: PLW0406 |
| 91 | |
| 92 | monkeypatch.setattr(timing, "sleep", self.sleep) |
| 93 | monkeypatch.setattr(timing, "time", self.time) |
| 94 | monkeypatch.setattr(timing, "perf_counter", self.time) |
| 95 | |
| 96 | |
| 97 | __all__ = ["perf_counter", "sleep", "time"] |
no outgoing calls
searching dependent graphs…