Skip to content

Commit 6106bbe

Browse files
committed
Use context managers for patching in test_executionmocking.py
1 parent 8023695 commit 6106bbe

File tree

1 file changed

+13
-14
lines changed

1 file changed

+13
-14
lines changed

tests/testcase/execution/test_executionmocking.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,24 @@ def test_make_deterministic_reseeds_tracked_instances():
2525
config.configuration.seeding.seed = 99
2626
mock_inst = MagicMock(spec=random.Random) # noqa: S311
2727
tracked: weakref.WeakSet = weakref.WeakSet([mock_inst])
28-
orig_seed = random.Random.seed
29-
try:
28+
with (
29+
mock.patch("random.Random.seed"),
30+
mock.patch("random.seed"),
31+
):
3032
random.Random.seed.__pynguin_instances__ = tracked
31-
with mock.patch("random.seed"):
32-
execution._make_deterministic()
33-
mock_inst.seed.assert_called_once_with(99)
34-
finally:
35-
random.Random.seed = orig_seed
33+
execution._make_deterministic()
34+
mock_inst.seed.assert_called_once_with(99)
3635

3736

3837
def test_make_deterministic_excludes_pynguin_rng():
3938
"""Pynguin's own RNG must not be reseeded by _make_deterministic."""
4039
config.configuration.seeding.seed = 7
4140
tracked: weakref.WeakSet = weakref.WeakSet([_rnd.RNG])
42-
orig_seed = random.Random.seed
43-
try:
41+
with (
42+
mock.patch("random.Random.seed"),
43+
mock.patch.object(_rnd.RNG, "seed") as rng_seed_mock,
44+
mock.patch("random.seed"),
45+
):
4446
random.Random.seed.__pynguin_instances__ = tracked
45-
with mock.patch.object(_rnd.RNG, "seed") as rng_seed_mock, mock.patch("random.seed"):
46-
execution._make_deterministic()
47-
rng_seed_mock.assert_not_called()
48-
finally:
49-
random.Random.seed = orig_seed
47+
execution._make_deterministic()
48+
rng_seed_mock.assert_not_called()

0 commit comments

Comments
 (0)