Skip to content

Commit

Permalink
Properly mock logger in pre-start script tests
Browse files Browse the repository at this point in the history
#3
#4
https://docs.python.org/3/library/unittest.mock.html#where-to-patch

- Mock the root logger object directly: The project previously had a
  `mock_logger` pytest fixture in conftest.py that instantiated a root
  logger with `logging.getLogger()`. The individual log level attributes
  were then patched with `mocker.patch.object(logger, "debug")`. This
  was problematic, because Mypy thought that the logger didn't have the
  attributes, requiring many `# type: ignore[attr-defined]` comments.
  Instead of `logging.getLogger()`, the root logger object itself will
  be directly patched whenever it is used for testing.
- Remove `# type: ignore[attr-defined]` Mypy comments: now that the
  `mock_logger` is a proper `MockerFixture`, pytest and pytest-mock
  will create the necessary attributes automatically.
  • Loading branch information
br3ndonland committed Mar 6, 2021
1 parent 7b65853 commit 23682a1
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions tests/test_start.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,16 +363,16 @@ class TestRunPreStartScript:

def test_run_pre_start_script_py(
self,
mock_logger: logging.Logger,
mocker: MockerFixture,
monkeypatch: pytest.MonkeyPatch,
pre_start_script_tmp_py: Path,
) -> None:
"""Test `start.run_pre_start_script` using temporary Python pre-start script."""
mock_logger = mocker.patch.object(start.logging, "root", autospec=True)
monkeypatch.setenv("PRE_START_PATH", str(pre_start_script_tmp_py))
pre_start_path = os.getenv("PRE_START_PATH")
start.run_pre_start_script(logger=mock_logger)
mock_logger.debug.assert_has_calls( # type: ignore[attr-defined]
mock_logger.debug.assert_has_calls(
calls=[
mocker.call("Checking for pre-start script."),
mocker.call(f"Running pre-start script with python {pre_start_path}."),
Expand All @@ -382,16 +382,16 @@ def test_run_pre_start_script_py(

def test_run_pre_start_script_sh(
self,
mock_logger: logging.Logger,
mocker: MockerFixture,
monkeypatch: pytest.MonkeyPatch,
pre_start_script_tmp_sh: Path,
) -> None:
"""Test `start.run_pre_start_script` using temporary pre-start shell script."""
mock_logger = mocker.patch.object(start.logging, "root", autospec=True)
monkeypatch.setenv("PRE_START_PATH", str(pre_start_script_tmp_sh))
pre_start_path = os.getenv("PRE_START_PATH")
start.run_pre_start_script(logger=mock_logger)
mock_logger.debug.assert_has_calls( # type: ignore[attr-defined]
mock_logger.debug.assert_has_calls(
calls=[
mocker.call("Checking for pre-start script."),
mocker.call(f"Running pre-start script with sh {pre_start_path}."),
Expand All @@ -401,14 +401,14 @@ def test_run_pre_start_script_sh(

def test_run_pre_start_script_no_file(
self,
mock_logger: logging.Logger,
mocker: MockerFixture,
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""Test `start.run_pre_start_script` with an incorrect file path."""
mock_logger = mocker.patch.object(start.logging, "root", autospec=True)
monkeypatch.setenv("PRE_START_PATH", "/no/file/here")
start.run_pre_start_script(logger=mock_logger)
mock_logger.debug.assert_has_calls( # type: ignore[attr-defined]
mock_logger.debug.assert_has_calls(
calls=[
mocker.call("Checking for pre-start script."),
mocker.call("No pre-start script found."),
Expand Down

0 comments on commit 23682a1

Please sign in to comment.