From 2d18a4cefff6f26a48966e66774c463dfbbe2492 Mon Sep 17 00:00:00 2001 From: Cody Logan Date: Mon, 6 Nov 2023 12:06:08 -0800 Subject: Be more consistent in use of mocking method --- tests/test_wikiget_cli.py | 45 ++++++++++++++++++++++++++------------------- 1 file changed, 26 insertions(+), 19 deletions(-) (limited to 'tests/test_wikiget_cli.py') diff --git a/tests/test_wikiget_cli.py b/tests/test_wikiget_cli.py index 86b3780..0306579 100644 --- a/tests/test_wikiget_cli.py +++ b/tests/test_wikiget_cli.py @@ -15,6 +15,8 @@ # You should have received a copy of the GNU General Public License # along with Wikiget. If not, see . +from unittest.mock import MagicMock, patch + import pytest from wikiget import USER_AGENT, __version__ @@ -23,47 +25,52 @@ from wikiget.wikiget import cli class TestCli: def test_cli_no_params(self, monkeypatch: pytest.MonkeyPatch) -> None: - monkeypatch.setattr("sys.argv", ["wikiget"]) - with pytest.raises(SystemExit) as e: - cli() - assert e.value.code == 2 + with monkeypatch.context() as m: + m.setattr("sys.argv", ["wikiget"]) + with pytest.raises(SystemExit) as e: + cli() + assert e.value.code == 2 - def test_cli_completed_successfully(self, monkeypatch: pytest.MonkeyPatch) -> None: - def mock_process_download(*args, **kwargs) -> int: # noqa: ARG001 - """A successful call to process_download returns 0.""" - return 0 + @patch("wikiget.wikiget.process_download") + def test_cli_completed_successfully( + self, mock_process_download: MagicMock, monkeypatch: pytest.MonkeyPatch + ) -> None: + # a successful call to process_download returns 0 + mock_process_download.return_value = 0 with monkeypatch.context() as m: m.setattr("sys.argv", ["wikiget", "File:Example.jpg"]) - m.setattr("wikiget.wikiget.process_download", mock_process_download) with pytest.raises(SystemExit) as e: cli() assert e.value.code == 0 - def test_cli_completed_with_problems(self, monkeypatch: pytest.MonkeyPatch) -> None: - def mock_process_download(*args, **kwargs) -> int: # noqa: ARG001 - """An unsuccessful call to process_download returns 1.""" - return 1 + @patch("wikiget.wikiget.process_download") + def test_cli_completed_with_problems( + self, mock_process_download: MagicMock, monkeypatch: pytest.MonkeyPatch + ) -> None: + # an unsuccessful call to process_download returns 1 + mock_process_download.return_value = 1 with monkeypatch.context() as m: m.setattr("sys.argv", ["wikiget", "File:Example.jpg"]) - m.setattr("wikiget.wikiget.process_download", mock_process_download) with pytest.raises(SystemExit) as e: cli() assert e.value.code == 1 + @patch("wikiget.wikiget.process_download") def test_cli_logs( - self, monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture + self, + mock_process_download: MagicMock, + monkeypatch: pytest.MonkeyPatch, + caplog: pytest.LogCaptureFixture, ) -> None: - def mock_process_download(*args, **kwargs) -> int: # noqa: ARG001 - """A successful call to process_download returns 0.""" - return 0 + # a successful call to process_download returns 0 + mock_process_download.return_value = 0 with monkeypatch.context() as m: m.setattr("sys.argv", ["wikiget", "File:Example.jpg"]) - m.setattr("wikiget.wikiget.process_download", mock_process_download) with pytest.raises(SystemExit): cli() -- cgit v1.2.3