Skip to content

Commit b3d9f1f

Browse files
committed
Add separate test file for fixtures' return annotations
1 parent 40d6a7c commit b3d9f1f

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
from collections.abc import Callable
2+
from typing import Any
3+
4+
from _pytest.config import ExitCode
5+
from _pytest.fixtures import get_return_annotation
6+
from _pytest.pytester import Pytester
7+
8+
9+
class TestGetReturnAnnotation:
10+
def test_primitive_return_type(self):
11+
def six() -> int:
12+
return 6
13+
14+
assert get_return_annotation(six) == "int"
15+
16+
def test_compound_return_type(self):
17+
def two_sixes() -> tuple[int, str]:
18+
return (6, "six")
19+
20+
assert get_return_annotation(two_sixes) == "tuple[int, str]"
21+
22+
def test_callable_return_type(self):
23+
def callable_return() -> Callable[..., Any]:
24+
return self.test_compound_return_type
25+
26+
assert get_return_annotation(callable_return) == "Callable[..., Any]"
27+
28+
def test_no_annotation(self):
29+
def no_annotation():
30+
return 6
31+
32+
assert get_return_annotation(no_annotation) == ""
33+
34+
def test_none_return_type(self):
35+
def none_return() -> None:
36+
pass
37+
38+
assert get_return_annotation(none_return) == "None"
39+
40+
def test_custom_class_return_type(self):
41+
class T:
42+
pass
43+
44+
def class_return() -> T:
45+
return T()
46+
47+
assert get_return_annotation(class_return) == "T"
48+
49+
def test_enum_return_type(self):
50+
def enum_return() -> ExitCode:
51+
return ExitCode(0)
52+
53+
assert get_return_annotation(enum_return) == "ExitCode"
54+
55+
def test_with_arg_annotations(self):
56+
def with_args(a: Callable[[], None], b: list) -> range:
57+
return range(2)
58+
59+
assert get_return_annotation(with_args) == "range"
60+
61+
def test_invalid_return_type(self):
62+
def bad_annotation() -> 6: # type: ignore
63+
return 6
64+
65+
assert get_return_annotation(bad_annotation) == "6"
66+
67+
def test_unobtainable_signature(self):
68+
assert get_return_annotation(len) == ""
69+
70+
71+
def test_fixtures_return_annotation(pytester: Pytester) -> None:
72+
p = pytester.makepyfile(
73+
"""
74+
import pytest
75+
@pytest.fixture
76+
def six() -> int:
77+
return 6
78+
"""
79+
)
80+
result = pytester.runpytest("--fixtures", p)
81+
result.stdout.fnmatch_lines(
82+
"""
83+
*fixtures defined from*
84+
*six -> int -- test_fixtures_return_annotation.py:3*
85+
"""
86+
)
87+
88+
89+
def test_fixtures_per_test_return_annotation(pytester: Pytester) -> None:
90+
p = pytester.makepyfile(
91+
"""
92+
import pytest
93+
@pytest.fixture
94+
def five() -> int:
95+
return 5
96+
def test_five(five):
97+
pass
98+
"""
99+
)
100+
101+
result = pytester.runpytest("--fixtures-per-test", p)
102+
assert result.ret == 0
103+
104+
result.stdout.fnmatch_lines(
105+
[
106+
"*fixtures used by test_five*",
107+
"*(test_fixtures_per_test_return_annotation.py:6)*",
108+
"five -> int -- test_fixtures_per_test_return_annotation.py:3",
109+
]
110+
)

0 commit comments

Comments
 (0)