Skip to content

Commit 6d6ae27

Browse files
committed
testing: improve mocking in terminalprogress tests
1 parent 2ffef56 commit 6d6ae27

File tree

1 file changed

+23
-22
lines changed

1 file changed

+23
-22
lines changed

testing/test_terminal.py

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
from typing import cast
1313
from typing import Literal
1414
from typing import NamedTuple
15-
from unittest.mock import Mock
16-
from unittest.mock import patch
15+
from unittest import mock
1716

1817
import pluggy
1918

@@ -3419,45 +3418,47 @@ def mock_file(self) -> StringIO:
34193418

34203419
@pytest.fixture
34213420
def mock_tr(self, mock_file: StringIO) -> pytest.TerminalReporter:
3422-
tr = Mock(spec=pytest.TerminalReporter)
3421+
tr: pytest.TerminalReporter = mock.create_autospec(pytest.TerminalReporter)
34233422

3424-
def write_raw(s: str, *, flush: bool = False) -> None:
3425-
mock_file.write(s)
3423+
def write_raw(content: str, *, flush: bool = False) -> None:
3424+
mock_file.write(content)
34263425

3427-
tr.write_raw = write_raw
3426+
tr.write_raw = write_raw # type: ignore[method-assign]
34283427
tr._progress_nodeids_reported = set()
34293428
return tr
34303429

34313430
@pytest.mark.skipif(sys.platform != "win32", reason="#13896")
34323431
def test_plugin_registration_enabled_by_default(
3433-
self, pytester: pytest.Pytester
3432+
self, pytester: pytest.Pytester, monkeypatch: MonkeyPatch
34343433
) -> None:
34353434
"""Test that the plugin registration is enabled by default.
34363435
34373436
Currently only on Windows (#13896).
34383437
"""
3438+
monkeypatch.setattr(sys.stdout, "isatty", lambda: True)
34393439
# The plugin module should be registered as a default plugin.
3440-
with patch.object(sys.stdout, "isatty", return_value=True):
3441-
config = pytester.parseconfigure()
3442-
plugin = config.pluginmanager.get_plugin("terminalprogress")
3443-
assert plugin is not None
3440+
config = pytester.parseconfigure()
3441+
plugin = config.pluginmanager.get_plugin("terminalprogress")
3442+
assert plugin is not None
34443443

34453444
def test_plugin_registred_on_all_platforms_when_explicitly_requested(
3446-
self, pytester: pytest.Pytester
3445+
self, pytester: pytest.Pytester, monkeypatch: MonkeyPatch
34473446
) -> None:
34483447
"""Test that the plugin is registered on any platform if explicitly requested."""
3448+
monkeypatch.setattr(sys.stdout, "isatty", lambda: True)
34493449
# The plugin module should be registered as a default plugin.
3450-
with patch.object(sys.stdout, "isatty", return_value=True):
3451-
config = pytester.parseconfigure("-p", "terminalprogress")
3452-
plugin = config.pluginmanager.get_plugin("terminalprogress")
3453-
assert plugin is not None
3450+
config = pytester.parseconfigure("-p", "terminalprogress")
3451+
plugin = config.pluginmanager.get_plugin("terminalprogress")
3452+
assert plugin is not None
34543453

3455-
def test_disabled_for_non_tty(self, pytester: pytest.Pytester) -> None:
3454+
def test_disabled_for_non_tty(
3455+
self, pytester: pytest.Pytester, monkeypatch: MonkeyPatch
3456+
) -> None:
34563457
"""Test that plugin is disabled for non-TTY output."""
3457-
with patch.object(sys.stdout, "isatty", return_value=False):
3458-
config = pytester.parseconfigure("-p", "terminalprogress")
3459-
plugin = config.pluginmanager.get_plugin("terminalprogress-plugin")
3460-
assert plugin is None
3458+
monkeypatch.setattr(sys.stdout, "isatty", lambda: False)
3459+
config = pytester.parseconfigure("-p", "terminalprogress")
3460+
plugin = config.pluginmanager.get_plugin("terminalprogress-plugin")
3461+
assert plugin is None
34613462

34623463
@pytest.mark.parametrize(
34633464
["state", "progress", "expected"],
@@ -3489,7 +3490,7 @@ def test_session_lifecycle(
34893490
"""Test progress updates during session lifecycle."""
34903491
plugin = TerminalProgressPlugin(mock_tr)
34913492

3492-
session = Mock(spec=pytest.Session)
3493+
session = mock.create_autospec(pytest.Session)
34933494
session.testscollected = 3
34943495

34953496
# Session start - should emit indeterminate progress.

0 commit comments

Comments
 (0)