示例工程 29-testing-pytest

29-testing-pytest/04-patch-target/test_patch_target.py

python · 0.8 KB

"""test_patch_target.py"""
 
from __future__ import annotations
 
from unittest.mock import patch
 
import data_service
import pytest
from patch_target import main
 
 
def test_wrong_target_does_not_affect_service() -> None:
    with patch("data_client.fetch", return_value="mock"):
        assert data_service.get_data() == "real"
 
 
def test_right_target_patches_usage() -> None:
    with patch("data_service.fetch", return_value="mock"):
        assert data_service.get_data() == "mock"
 
 
def test_autospec_rejects_bad_signature() -> None:
    with patch("data_service.fetch", autospec=True) as fake:
        fake.return_value = "mock"
        assert data_service.get_data() == "mock"
    fake.assert_called_once_with()
 
 
def test_main_runs(capsys: pytest.CaptureFixture[str]) -> None:
    main()
    assert capsys.readouterr().out == "data: real\n"