Skip to content

Commit 061fe2a

Browse files
MarieRoaldyngvem
andcommitted
Improve usage of unittest.mock.patch
Co-authored-by: Yngve Mardal Moe <[email protected]>
1 parent 11d9bc3 commit 061fe2a

File tree

1 file changed

+7
-12
lines changed

1 file changed

+7
-12
lines changed

Lib/test/test_turtle.py

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import os
22
import pickle
33
import re
4-
import sys
54
import tempfile
65
import unittest
76
import unittest.mock
8-
from contextlib import contextmanager
97
from test import support
108
from test.support import import_helper
119
from test.support import os_helper
@@ -56,22 +54,19 @@
5654
"""
5755

5856

59-
@contextmanager
6057
def patch_screen():
6158
"""Patch turtle._Screen for testing without a display.
6259
6360
We must patch the _Screen class itself instead of the _Screen
6461
instance because instantiating it requires a display.
6562
"""
66-
m = unittest.mock.MagicMock()
67-
m.__class__ = turtle._Screen
68-
m.mode.return_value = "standard"
69-
70-
patch = unittest.mock.patch('turtle._Screen.__new__', return_value=m)
71-
try:
72-
yield patch.__enter__()
73-
finally:
74-
patch.__exit__(*sys.exc_info())
63+
return unittest.mock.patch(
64+
"turtle._Screen.__new__",
65+
**{
66+
"return_value.__class__": turtle._Screen,
67+
"return_value.mode.return_value": "standard",
68+
},
69+
)
7570

7671

7772
class TurtleConfigTest(unittest.TestCase):

0 commit comments

Comments
 (0)