From e731e35e6cd71fce18773ffb555f46628727f323 Mon Sep 17 00:00:00 2001 From: Nikita Kodenko Date: Thu, 22 Jul 2021 22:06:54 +0700 Subject: [PATCH] tests: moved few unittest tests to pytest (#6339) * tests: moved few unittest tests to pytest * tests: unittest.mock -> pytest-mock in few tests * tests: test_prompt: unittest.mock -> pytest-mock * Update tests/unit/test_daemon.py Co-authored-by: Saugat Pachhai Co-authored-by: Saugat Pachhai --- tests/unit/test_daemon.py | 52 +++++++++++++++++++-------------------- tests/unit/test_prompt.py | 17 ++++++------- 2 files changed, 32 insertions(+), 37 deletions(-) diff --git a/tests/unit/test_daemon.py b/tests/unit/test_daemon.py index d56797203..4e4d0592d 100644 --- a/tests/unit/test_daemon.py +++ b/tests/unit/test_daemon.py @@ -1,38 +1,36 @@ import inspect import os -from unittest import TestCase, mock from dvc import daemon -class TestDaemon(TestCase): - @mock.patch("dvc.daemon._spawn_posix") - @mock.patch("dvc.daemon._spawn_windows") - def test(self, mock_windows, mock_posix): - daemon.daemon(["updater"]) +def test_daemon(mocker): + mock_windows = mocker.patch("dvc.daemon._spawn_windows") + mock_posix = mocker.patch("dvc.daemon._spawn_posix") + daemon.daemon(["updater"]) - if os.name == "nt": - mock_posix.assert_not_called() - mock_windows.assert_called() - args = mock_windows.call_args[0] - else: - mock_windows.assert_not_called() - mock_posix.assert_called() - args = mock_posix.call_args[0] + if os.name == "nt": + mock_posix.assert_not_called() + mock_windows.assert_called() + args = mock_windows.call_args[0] + else: + mock_windows.assert_not_called() + mock_posix.assert_called() + args = mock_posix.call_args[0] - env = args[1] - self.assertTrue("PYTHONPATH" in env.keys()) + env = args[1] + assert "PYTHONPATH" in env.keys() - file_path = os.path.abspath(inspect.stack()[0][1]) - file_dir = os.path.dirname(file_path) - test_dir = os.path.dirname(file_dir) - dvc_dir = os.path.dirname(test_dir) - self.assertEqual(env["PYTHONPATH"], dvc_dir) - self.assertEqual(env[daemon.DVC_DAEMON], "1") + file_path = os.path.abspath(inspect.stack()[0][1]) + file_dir = os.path.dirname(file_path) + test_dir = os.path.dirname(file_dir) + dvc_dir = os.path.dirname(test_dir) + assert env["PYTHONPATH"] == dvc_dir + assert env[daemon.DVC_DAEMON] == "1" -def test_no_recursive_spawn(): - with mock.patch.dict(os.environ, {daemon.DVC_DAEMON: "1"}): - with mock.patch("dvc.daemon._spawn") as mock_spawn: - daemon.daemon(["updater"]) - mock_spawn.assert_not_called() +def test_no_recursive_spawn(mocker): + mocker.patch.dict(os.environ, {daemon.DVC_DAEMON: "1"}) + mock_spawn = mocker.patch("dvc.daemon._spawn") + daemon.daemon(["updater"]) + mock_spawn.assert_not_called() diff --git a/tests/unit/test_prompt.py b/tests/unit/test_prompt.py index 1a29a08d7..39bf4311b 100644 --- a/tests/unit/test_prompt.py +++ b/tests/unit/test_prompt.py @@ -1,13 +1,10 @@ -from unittest import TestCase, mock - from dvc.prompt import confirm -class TestConfirm(TestCase): - @mock.patch("sys.stdout.isatty", return_value=True) - @mock.patch("dvc.prompt.input", side_effect=EOFError) - def test_eof(self, mock_input, mock_isatty): - ret = confirm("message") - mock_isatty.assert_called() - mock_input.assert_called() - self.assertFalse(ret) +def test_confirm_in_tty_if_stdin_is_closed(mocker): + mock_input = mocker.patch("dvc.prompt.input", side_effect=EOFError) + mock_isatty = mocker.patch("sys.stdout.isatty", return_value=True) + ret = confirm("message") + mock_isatty.assert_called() + mock_input.assert_called() + assert not ret