From 08a5907bd8b34e2f99a0c74e6756c66134ceb7d3 Mon Sep 17 00:00:00 2001 From: Cody Logan Date: Fri, 3 Nov 2023 10:40:07 -0700 Subject: Move from unittest.mock to pytest's monkeypatch where feasible --- tests/test_wikiget_cli.py | 84 +++++++++++++++++++++++++---------------------- 1 file changed, 44 insertions(+), 40 deletions(-) (limited to 'tests/test_wikiget_cli.py') diff --git a/tests/test_wikiget_cli.py b/tests/test_wikiget_cli.py index 99d113e..86b3780 100644 --- a/tests/test_wikiget_cli.py +++ b/tests/test_wikiget_cli.py @@ -15,55 +15,59 @@ # 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__ from wikiget.wikiget import cli -def test_cli_no_params(monkeypatch: pytest.MonkeyPatch): - monkeypatch.setattr("sys.argv", ["wikiget"]) - with pytest.raises(SystemExit) as e: - cli() - assert e.value.code == 2 +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 + + 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 + + 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_successfully( - mock_process_download: MagicMock, monkeypatch: pytest.MonkeyPatch -) -> None: - monkeypatch.setattr("sys.argv", ["wikiget", "File:Example.jpg"]) - mock_process_download.return_value = 0 - with pytest.raises(SystemExit) as e: - cli() - assert mock_process_download.called - assert e.value.code == 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 == 1 -@patch("wikiget.wikiget.process_download") -def test_cli_completed_with_problems( - mock_process_download: MagicMock, monkeypatch: pytest.MonkeyPatch -) -> None: - monkeypatch.setattr("sys.argv", ["wikiget", "File:Example.jpg"]) - mock_process_download.return_value = 1 - with pytest.raises(SystemExit) as e: - cli() - assert mock_process_download.called - assert e.value.code == 1 + def test_cli_logs( + self, 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 + with monkeypatch.context() as m: + m.setattr("sys.argv", ["wikiget", "File:Example.jpg"]) + m.setattr("wikiget.wikiget.process_download", mock_process_download) -@patch("wikiget.wikiget.process_download") -def test_cli_logs( - mock_process_download: MagicMock, - monkeypatch: pytest.MonkeyPatch, - caplog: pytest.LogCaptureFixture, -) -> None: - monkeypatch.setattr("sys.argv", ["wikiget", "File:Example.jpg"]) - mock_process_download.return_value = 0 - with pytest.raises(SystemExit): - cli() - assert mock_process_download.called - assert f"Starting download session using wikiget {__version__}" in caplog.text - assert USER_AGENT in caplog.text + with pytest.raises(SystemExit): + cli() + assert ( + f"Starting download session using wikiget {__version__}" in caplog.text + ) + assert USER_AGENT in caplog.text -- cgit v1.2.3 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