| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from _pytest.pytester import Pytester |
| 4 |
|
| 5 |
|
| 6 |
def test_should_show_no_output_when_zero_items(pytester: Pytester) -> None: |
| 7 |
result = pytester.runpytest("--fixtures-per-test") |
| 8 |
result.stdout.no_fnmatch_line("*fixtures used by*") |
| 9 |
assert result.ret == 0 |
| 10 |
|
| 11 |
|
| 12 |
def test_fixtures_in_module(pytester: Pytester) -> None: |
| 13 |
p = pytester.makepyfile( |
| 14 |
''' |
| 15 |
import pytest |
| 16 |
@pytest.fixture |
| 17 |
def _arg0(): |
| 18 |
"""hidden arg0 fixture""" |
| 19 |
@pytest.fixture |
| 20 |
def arg1(): |
| 21 |
"""arg1 docstring""" |
| 22 |
def test_arg1(arg1): |
| 23 |
pass |
| 24 |
''' |
| 25 |
) |
| 26 |
|
| 27 |
result = pytester.runpytest("--fixtures-per-test", p) |
| 28 |
assert result.ret == 0 |
| 29 |
|
| 30 |
result.stdout.fnmatch_lines( |
| 31 |
[ |
| 32 |
"*fixtures used by test_arg1*", |
| 33 |
"*(test_fixtures_in_module.py:9)*", |
| 34 |
"arg1 -- test_fixtures_in_module.py:6", |
| 35 |
" arg1 docstring", |
| 36 |
] |
| 37 |
) |
| 38 |
result.stdout.no_fnmatch_line("*_arg0*") |
| 39 |
|
| 40 |
|
| 41 |
def test_fixtures_in_conftest(pytester: Pytester) -> None: |
| 42 |
pytester.makeconftest( |
| 43 |
''' |
| 44 |
import pytest |
| 45 |
@pytest.fixture |
| 46 |
def arg1(): |
| 47 |
"""arg1 docstring""" |
| 48 |
@pytest.fixture |
| 49 |
def arg2(): |
| 50 |
"""arg2 docstring""" |
| 51 |
@pytest.fixture |
| 52 |
def arg3(arg1, arg2): |
| 53 |
"""arg3 |
| 54 |
docstring |
| 55 |
""" |
| 56 |
''' |
| 57 |
) |
| 58 |
p = pytester.makepyfile( |
| 59 |
""" |
| 60 |
def test_arg2(arg2): |
| 61 |
pass |
| 62 |
def test_arg3(arg3): |
| 63 |
pass |
| 64 |
""" |
| 65 |
) |
| 66 |
result = pytester.runpytest("--fixtures-per-test", p) |
| 67 |
assert result.ret == 0 |
| 68 |
|
| 69 |
result.stdout.fnmatch_lines( |
| 70 |
[ |
| 71 |
"*fixtures used by test_arg2*", |
| 72 |
"*(test_fixtures_in_conftest.py:2)*", |
| 73 |
"arg2 -- conftest.py:6", |
| 74 |
" arg2 docstring", |
| 75 |
"*fixtures used by test_arg3*", |
| 76 |
"*(test_fixtures_in_conftest.py:4)*", |
| 77 |
"arg1 -- conftest.py:3", |
| 78 |
" arg1 docstring", |
| 79 |
"arg2 -- conftest.py:6", |
| 80 |
" arg2 docstring", |
| 81 |
"arg3 -- conftest.py:9", |
| 82 |
" arg3", |
| 83 |
] |
| 84 |
) |
| 85 |
|
| 86 |
|
| 87 |
def test_should_show_fixtures_used_by_test(pytester: Pytester) -> None: |
| 88 |
pytester.makeconftest( |
| 89 |
''' |
| 90 |
import pytest |
| 91 |
@pytest.fixture |
| 92 |
def arg1(): |
| 93 |
"""arg1 from conftest""" |
| 94 |
@pytest.fixture |
| 95 |
def arg2(): |
| 96 |
"""arg2 from conftest""" |
| 97 |
''' |
| 98 |
) |
| 99 |
p = pytester.makepyfile( |
| 100 |
''' |
| 101 |
import pytest |
| 102 |
@pytest.fixture |
| 103 |
def arg1(): |
| 104 |
"""arg1 from testmodule""" |
| 105 |
def test_args(arg1, arg2): |
| 106 |
pass |
| 107 |
''' |
| 108 |
) |
| 109 |
result = pytester.runpytest("--fixtures-per-test", p) |
| 110 |
assert result.ret == 0 |
| 111 |
|
| 112 |
result.stdout.fnmatch_lines( |
| 113 |
[ |
| 114 |
"*fixtures used by test_args*", |
| 115 |
"*(test_should_show_fixtures_used_by_test.py:6)*", |
| 116 |
"arg1 -- test_should_show_fixtures_used_by_test.py:3", |
| 117 |
" arg1 from testmodule", |
| 118 |
"arg2 -- conftest.py:6", |
| 119 |
" arg2 from conftest", |
| 120 |
] |
| 121 |
) |
| 122 |
|
| 123 |
|
| 124 |
def test_verbose_include_private_fixtures_and_loc(pytester: Pytester) -> None: |
| 125 |
pytester.makeconftest( |
| 126 |
''' |
| 127 |
import pytest |
| 128 |
@pytest.fixture |
| 129 |
def _arg1(): |
| 130 |
"""_arg1 from conftest""" |
| 131 |
@pytest.fixture |
| 132 |
def arg2(_arg1): |
| 133 |
"""arg2 from conftest""" |
| 134 |
''' |
| 135 |
) |
| 136 |
p = pytester.makepyfile( |
| 137 |
''' |
| 138 |
import pytest |
| 139 |
@pytest.fixture |
| 140 |
def arg3(): |
| 141 |
"""arg3 from testmodule""" |
| 142 |
def test_args(arg2, arg3): |
| 143 |
pass |
| 144 |
''' |
| 145 |
) |
| 146 |
result = pytester.runpytest("--fixtures-per-test", "-v", p) |
| 147 |
assert result.ret == 0 |
| 148 |
|
| 149 |
result.stdout.fnmatch_lines( |
| 150 |
[ |
| 151 |
"*fixtures used by test_args*", |
| 152 |
"*(test_verbose_include_private_fixtures_and_loc.py:6)*", |
| 153 |
"_arg1 -- conftest.py:3", |
| 154 |
" _arg1 from conftest", |
| 155 |
"arg2 -- conftest.py:6", |
| 156 |
" arg2 from conftest", |
| 157 |
"arg3 -- test_verbose_include_private_fixtures_and_loc.py:3", |
| 158 |
" arg3 from testmodule", |
| 159 |
] |
| 160 |
) |
| 161 |
|
| 162 |
|
| 163 |
def test_doctest_items(pytester: Pytester) -> None: |
| 164 |
pytester.makepyfile( |
| 165 |
''' |
| 166 |
def foo(): |
| 167 |
""" |
| 168 |
>>> 1 + 1 |
| 169 |
2 |
| 170 |
""" |
| 171 |
''' |
| 172 |
) |
| 173 |
pytester.maketxtfile( |
| 174 |
""" |
| 175 |
>>> 1 + 1 |
| 176 |
2 |
| 177 |
""" |
| 178 |
) |
| 179 |
result = pytester.runpytest( |
| 180 |
"--fixtures-per-test", "--doctest-modules", "--doctest-glob=*.txt", "-v" |
| 181 |
) |
| 182 |
assert result.ret == 0 |
| 183 |
|
| 184 |
result.stdout.fnmatch_lines(["*collected 2 items*"]) |
| 185 |
|
| 186 |
|
| 187 |
def test_multiline_docstring_in_module(pytester: Pytester) -> None: |
| 188 |
p = pytester.makepyfile( |
| 189 |
''' |
| 190 |
import pytest |
| 191 |
@pytest.fixture |
| 192 |
def arg1(): |
| 193 |
"""Docstring content that spans across multiple lines, |
| 194 |
through second line, |
| 195 |
and through third line. |
| 196 |
|
| 197 |
Docstring content that extends into a second paragraph. |
| 198 |
|
| 199 |
Docstring content that extends into a third paragraph. |
| 200 |
""" |
| 201 |
def test_arg1(arg1): |
| 202 |
pass |
| 203 |
''' |
| 204 |
) |
| 205 |
|
| 206 |
result = pytester.runpytest("--fixtures-per-test", p) |
| 207 |
assert result.ret == 0 |
| 208 |
|
| 209 |
result.stdout.fnmatch_lines( |
| 210 |
[ |
| 211 |
"*fixtures used by test_arg1*", |
| 212 |
"*(test_multiline_docstring_in_module.py:13)*", |
| 213 |
"arg1 -- test_multiline_docstring_in_module.py:3", |
| 214 |
" Docstring content that spans across multiple lines,", |
| 215 |
" through second line,", |
| 216 |
" and through third line.", |
| 217 |
] |
| 218 |
) |
| 219 |
|
| 220 |
|
| 221 |
def test_verbose_include_multiline_docstring(pytester: Pytester) -> None: |
| 222 |
p = pytester.makepyfile( |
| 223 |
''' |
| 224 |
import pytest |
| 225 |
@pytest.fixture |
| 226 |
def arg1(): |
| 227 |
"""Docstring content that spans across multiple lines, |
| 228 |
through second line, |
| 229 |
and through third line. |
| 230 |
|
| 231 |
Docstring content that extends into a second paragraph. |
| 232 |
|
| 233 |
Docstring content that extends into a third paragraph. |
| 234 |
""" |
| 235 |
def test_arg1(arg1): |
| 236 |
pass |
| 237 |
''' |
| 238 |
) |
| 239 |
|
| 240 |
result = pytester.runpytest("--fixtures-per-test", "-v", p) |
| 241 |
assert result.ret == 0 |
| 242 |
|
| 243 |
result.stdout.fnmatch_lines( |
| 244 |
[ |
| 245 |
"*fixtures used by test_arg1*", |
| 246 |
"*(test_verbose_include_multiline_docstring.py:13)*", |
| 247 |
"arg1 -- test_verbose_include_multiline_docstring.py:3", |
| 248 |
" Docstring content that spans across multiple lines,", |
| 249 |
" through second line,", |
| 250 |
" and through third line.", |
| 251 |
" ", |
| 252 |
" Docstring content that extends into a second paragraph.", |
| 253 |
" ", |
| 254 |
" Docstring content that extends into a third paragraph.", |
| 255 |
] |
| 256 |
) |
| 257 |
|
| 258 |
|
| 259 |
def test_should_not_show_direct_param_fixtures(pytester: Pytester) -> None: |
| 260 |
"""A direct-param fixture is a helper fixture created as an implementation |
| 261 |
detail of direct parametrization. |
| 262 |
|
| 263 |
These fixtures should not be included in the output because they don't |
| 264 |
satisfy user expectations for how fixtures are created and used (#11295). |
| 265 |
""" |
| 266 |
pytester.makepyfile( |
| 267 |
""" |
| 268 |
import pytest |
| 269 |
|
| 270 |
@pytest.mark.parametrize("x", [1]) |
| 271 |
def test_pseudo_fixture(x): |
| 272 |
pass |
| 273 |
""" |
| 274 |
) |
| 275 |
result = pytester.runpytest("--fixtures-per-test") |
| 276 |
result.stdout.no_fnmatch_line("*fixtures used by*") |
| 277 |
assert result.ret == 0 |
| 278 |
|
| 279 |
|
| 280 |
def test_should_show_parametrized_fixtures_used_by_test(pytester: Pytester) -> None: |
| 281 |
"""A fixture with parameters should be included if it was created using |
| 282 |
the @pytest.fixture decorator, including those that are indirectly |
| 283 |
parametrized.""" |
| 284 |
pytester.makepyfile( |
| 285 |
''' |
| 286 |
import pytest |
| 287 |
|
| 288 |
@pytest.fixture(params=['a', 'b']) |
| 289 |
def directly(request): |
| 290 |
"""parametrized fixture""" |
| 291 |
return request.param |
| 292 |
|
| 293 |
@pytest.fixture |
| 294 |
def indirectly(request): |
| 295 |
"""indirectly parametrized fixture""" |
| 296 |
return request.param |
| 297 |
|
| 298 |
def test_directly_parametrized_fixture(directly): |
| 299 |
pass |
| 300 |
|
| 301 |
@pytest.mark.parametrize("indirectly", ["a", "b"], indirect=True) |
| 302 |
def test_indirectly_parametrized_fixture(indirectly): |
| 303 |
pass |
| 304 |
''' |
| 305 |
) |
| 306 |
result = pytester.runpytest("--fixtures-per-test") |
| 307 |
assert result.ret == 0 |
| 308 |
|
| 309 |
result.stdout.fnmatch_lines( |
| 310 |
[ |
| 311 |
"*fixtures used by test_directly_parametrized_fixture*", |
| 312 |
"*(test_should_show_parametrized_fixtures_used_by_test.py:14)*", |
| 313 |
"directly -- test_should_show_parametrized_fixtures_used_by_test.py:4", |
| 314 |
" parametrized fixture", |
| 315 |
"*fixtures used by test_directly_parametrized_fixture*", |
| 316 |
"*(test_should_show_parametrized_fixtures_used_by_test.py:14)*", |
| 317 |
"directly -- test_should_show_parametrized_fixtures_used_by_test.py:4", |
| 318 |
" parametrized fixture", |
| 319 |
"*fixtures used by test_indirectly_parametrized_fixture*", |
| 320 |
"*(test_should_show_parametrized_fixtures_used_by_test.py:17)*", |
| 321 |
"indirectly -- test_should_show_parametrized_fixtures_used_by_test.py:9", |
| 322 |
" indirectly parametrized fixture", |
| 323 |
"*fixtures used by test_indirectly_parametrized_fixture*", |
| 324 |
"*(test_should_show_parametrized_fixtures_used_by_test.py:17)*", |
| 325 |
"indirectly -- test_should_show_parametrized_fixtures_used_by_test.py:9", |
| 326 |
" indirectly parametrized fixture", |
| 327 |
] |
| 328 |
) |