Skip to content

Commit a00492a

Browse files
committed
✅ Add shared testing setup to mock print and access values
1 parent af52d65 commit a00492a

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

tests/conftest.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import shutil
22
import subprocess
33
import sys
4+
from dataclasses import dataclass, field
45
from pathlib import Path
5-
from typing import Any, Callable, Dict, List, Union
6+
from typing import Any, Callable, Dict, Generator, List, Union
7+
from unittest.mock import patch
68

79
import pytest
810
from pydantic import BaseModel
@@ -26,7 +28,7 @@ def clear_sqlmodel() -> Any:
2628

2729

2830
@pytest.fixture()
29-
def cov_tmp_path(tmp_path: Path):
31+
def cov_tmp_path(tmp_path: Path) -> Generator[Path, None, None]:
3032
yield tmp_path
3133
for coverage_path in tmp_path.glob(".coverage*"):
3234
coverage_destiny_path = top_level_path / coverage_path.name
@@ -53,8 +55,8 @@ def coverage_run(*, module: str, cwd: Union[str, Path]) -> subprocess.CompletedP
5355
def get_testing_print_function(
5456
calls: List[List[Union[str, Dict[str, Any]]]],
5557
) -> Callable[..., Any]:
56-
def new_print(*args):
57-
data = []
58+
def new_print(*args: Any) -> None:
59+
data: list[Any] = []
5860
for arg in args:
5961
if isinstance(arg, BaseModel):
6062
data.append(arg.model_dump())
@@ -71,6 +73,19 @@ def new_print(*args):
7173
return new_print
7274

7375

76+
@dataclass
77+
class PrintMock:
78+
calls: list[Any] = field(default_factory=list)
79+
80+
81+
@pytest.fixture(name="print_mock")
82+
def print_mock_fixture() -> Generator[PrintMock, None, None]:
83+
print_mock = PrintMock()
84+
new_print = get_testing_print_function(print_mock.calls)
85+
with patch("builtins.print", new=new_print):
86+
yield print_mock
87+
88+
7489
needs_pydanticv2 = pytest.mark.skipif(not IS_PYDANTIC_V2, reason="requires Pydantic v2")
7590
needs_pydanticv1 = pytest.mark.skipif(IS_PYDANTIC_V2, reason="requires Pydantic v1")
7691

0 commit comments

Comments
 (0)