Skip to content

Commit c9d5d57

Browse files
added deterministic patches for pytest_plugin
1 parent cef8503 commit c9d5d57

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

codeflash/verification/pytest_plugin.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,19 @@
1010
import re
1111
import sys
1212
import time
13+
import uuid
1314
import warnings
1415
from pathlib import Path
1516
from typing import TYPE_CHECKING, Any, Callable
16-
from unittest import TestCase
17+
from unittest import TestCase, mock
1718

1819
# PyTest Imports
1920
import pytest
2021
from pluggy import HookspecMarker
2122

2223
if TYPE_CHECKING:
24+
from collections.abc import Generator
25+
2326
from _pytest.config import Config, Parser
2427
from _pytest.main import Session
2528
from _pytest.python import Metafunc
@@ -138,6 +141,29 @@ def pytest_configure(config: Config) -> None:
138141
config.pluginmanager.register(PytestLoops(config), PytestLoops.name)
139142

140143

144+
@pytest.fixture(scope="session", autouse=True)
145+
def deterministic_environment() -> Generator[None, None, None]:
146+
with (
147+
mock.patch.multiple(
148+
"time",
149+
time=mock.MagicMock(return_value=1750778657.008457),
150+
monotonic=mock.MagicMock(side_effect=[1.0, 2.0, 3.0]),
151+
),
152+
mock.patch.multiple("random", random=mock.MagicMock(return_value=0.5), randint=mock.MagicMock(return_value=42)),
153+
mock.patch.multiple(
154+
"uuid",
155+
uuid1=mock.MagicMock(return_value=uuid.UUID("8f9b1416-5117-11f0-a1b5-c0b5d70a98e7")),
156+
uuid4=mock.MagicMock(return_value=uuid.UUID("81d8c509-bdc7-4fe8-b29d-ed3fdc7eacba")),
157+
),
158+
mock.patch(
159+
"time.sleep",
160+
return_value=None, # ignore sleep calls
161+
),
162+
mock.patch("time.perf_counter", return_value=123.456),
163+
):
164+
yield
165+
166+
141167
class PytestLoops:
142168
name: str = "pytest-loops"
143169

0 commit comments

Comments
 (0)