|
12 | 12 | from typing import cast |
13 | 13 | from typing import Literal |
14 | 14 | from typing import NamedTuple |
15 | | -from unittest.mock import Mock |
16 | | -from unittest.mock import patch |
| 15 | +from unittest import mock |
17 | 16 |
|
18 | 17 | import pluggy |
19 | 18 |
|
@@ -3419,45 +3418,47 @@ def mock_file(self) -> StringIO: |
3419 | 3418 |
|
3420 | 3419 | @pytest.fixture |
3421 | 3420 | def mock_tr(self, mock_file: StringIO) -> pytest.TerminalReporter: |
3422 | | - tr = Mock(spec=pytest.TerminalReporter) |
| 3421 | + tr: pytest.TerminalReporter = mock.create_autospec(pytest.TerminalReporter) |
3423 | 3422 |
|
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) |
3426 | 3425 |
|
3427 | | - tr.write_raw = write_raw |
| 3426 | + tr.write_raw = write_raw # type: ignore[method-assign] |
3428 | 3427 | tr._progress_nodeids_reported = set() |
3429 | 3428 | return tr |
3430 | 3429 |
|
3431 | 3430 | @pytest.mark.skipif(sys.platform != "win32", reason="#13896") |
3432 | 3431 | def test_plugin_registration_enabled_by_default( |
3433 | | - self, pytester: pytest.Pytester |
| 3432 | + self, pytester: pytest.Pytester, monkeypatch: MonkeyPatch |
3434 | 3433 | ) -> None: |
3435 | 3434 | """Test that the plugin registration is enabled by default. |
3436 | 3435 |
|
3437 | 3436 | Currently only on Windows (#13896). |
3438 | 3437 | """ |
| 3438 | + monkeypatch.setattr(sys.stdout, "isatty", lambda: True) |
3439 | 3439 | # 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 |
3444 | 3443 |
|
3445 | 3444 | def test_plugin_registred_on_all_platforms_when_explicitly_requested( |
3446 | | - self, pytester: pytest.Pytester |
| 3445 | + self, pytester: pytest.Pytester, monkeypatch: MonkeyPatch |
3447 | 3446 | ) -> None: |
3448 | 3447 | """Test that the plugin is registered on any platform if explicitly requested.""" |
| 3448 | + monkeypatch.setattr(sys.stdout, "isatty", lambda: True) |
3449 | 3449 | # 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 |
3454 | 3453 |
|
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: |
3456 | 3457 | """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 |
3461 | 3462 |
|
3462 | 3463 | @pytest.mark.parametrize( |
3463 | 3464 | ["state", "progress", "expected"], |
@@ -3489,7 +3490,7 @@ def test_session_lifecycle( |
3489 | 3490 | """Test progress updates during session lifecycle.""" |
3490 | 3491 | plugin = TerminalProgressPlugin(mock_tr) |
3491 | 3492 |
|
3492 | | - session = Mock(spec=pytest.Session) |
| 3493 | + session = mock.create_autospec(pytest.Session) |
3493 | 3494 | session.testscollected = 3 |
3494 | 3495 |
|
3495 | 3496 | # Session start - should emit indeterminate progress. |
|
0 commit comments