1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
|
# mypy: allow-untyped-defs
from __future__ import annotations
import sys
from _pytest.config import ExitCode
from _pytest.pytester import Pytester
import pytest
@pytest.fixture(params=["--setup-only", "--setup-plan", "--setup-show"], scope="module")
def mode(request):
return request.param
def test_show_only_active_fixtures(
pytester: Pytester, mode, dummy_yaml_custom_test
) -> None:
pytester.makepyfile(
'''
import pytest
@pytest.fixture
def _arg0():
"""hidden arg0 fixture"""
@pytest.fixture
def arg1():
"""arg1 docstring"""
def test_arg1(arg1):
pass
'''
)
result = pytester.runpytest(mode)
assert result.ret == 0
result.stdout.fnmatch_lines(
["*SETUP F arg1*", "*test_arg1 (fixtures used: arg1)*", "*TEARDOWN F arg1*"]
)
result.stdout.no_fnmatch_line("*_arg0*")
def test_show_different_scopes(pytester: Pytester, mode) -> None:
p = pytester.makepyfile(
'''
import pytest
@pytest.fixture
def arg_function():
"""function scoped fixture"""
@pytest.fixture(scope='session')
def arg_session():
"""session scoped fixture"""
def test_arg1(arg_session, arg_function):
pass
'''
)
result = pytester.runpytest(mode, p)
assert result.ret == 0
result.stdout.fnmatch_lines(
[
"SETUP S arg_session*",
"*SETUP F arg_function*",
"*test_arg1 (fixtures used: arg_function, arg_session)*",
"*TEARDOWN F arg_function*",
"TEARDOWN S arg_session*",
]
)
def test_show_nested_fixtures(pytester: Pytester, mode) -> None:
pytester.makeconftest(
'''
import pytest
@pytest.fixture(scope='session')
def arg_same():
"""session scoped fixture"""
'''
)
p = pytester.makepyfile(
'''
import pytest
@pytest.fixture(scope='function')
def arg_same(arg_same):
"""function scoped fixture"""
def test_arg1(arg_same):
pass
'''
)
result = pytester.runpytest(mode, p)
assert result.ret == 0
result.stdout.fnmatch_lines(
[
"SETUP S arg_same*",
"*SETUP F arg_same (fixtures used: arg_same)*",
"*test_arg1 (fixtures used: arg_same)*",
"*TEARDOWN F arg_same*",
"TEARDOWN S arg_same*",
]
)
def test_show_fixtures_with_autouse(pytester: Pytester, mode) -> None:
p = pytester.makepyfile(
'''
import pytest
@pytest.fixture
def arg_function():
"""function scoped fixture"""
@pytest.fixture(scope='session', autouse=True)
def arg_session():
"""session scoped fixture"""
def test_arg1(arg_function):
pass
'''
)
result = pytester.runpytest(mode, p)
assert result.ret == 0
result.stdout.fnmatch_lines(
[
"SETUP S arg_session*",
"*SETUP F arg_function*",
"*test_arg1 (fixtures used: arg_function, arg_session)*",
]
)
def test_show_fixtures_with_parameters(pytester: Pytester, mode) -> None:
pytester.makeconftest(
'''
import pytest
@pytest.fixture(scope='session', params=['foo', 'bar'])
def arg_same():
"""session scoped fixture"""
'''
)
p = pytester.makepyfile(
'''
import pytest
@pytest.fixture(scope='function')
def arg_other(arg_same):
"""function scoped fixture"""
def test_arg1(arg_other):
pass
'''
)
result = pytester.runpytest(mode, p)
assert result.ret == 0
result.stdout.fnmatch_lines(
[
"SETUP S arg_same?'foo'?",
"TEARDOWN S arg_same?'foo'?",
"SETUP S arg_same?'bar'?",
"TEARDOWN S arg_same?'bar'?",
]
)
def test_show_fixtures_with_parameter_ids(pytester: Pytester, mode) -> None:
pytester.makeconftest(
'''
import pytest
@pytest.fixture(
scope='session', params=['foo', 'bar'], ids=['spam', 'ham'])
def arg_same():
"""session scoped fixture"""
'''
)
p = pytester.makepyfile(
'''
import pytest
@pytest.fixture(scope='function')
def arg_other(arg_same):
"""function scoped fixture"""
def test_arg1(arg_other):
pass
'''
)
result = pytester.runpytest(mode, p)
assert result.ret == 0
result.stdout.fnmatch_lines(
["SETUP S arg_same?'spam'?", "SETUP S arg_same?'ham'?"]
)
def test_show_fixtures_with_parameter_ids_function(pytester: Pytester, mode) -> None:
p = pytester.makepyfile(
"""
import pytest
@pytest.fixture(params=['foo', 'bar'], ids=lambda p: p.upper())
def foobar():
pass
def test_foobar(foobar):
pass
"""
)
result = pytester.runpytest(mode, p)
assert result.ret == 0
result.stdout.fnmatch_lines(
["*SETUP F foobar?'FOO'?", "*SETUP F foobar?'BAR'?"]
)
def test_dynamic_fixture_request(pytester: Pytester) -> None:
p = pytester.makepyfile(
"""
import pytest
@pytest.fixture()
def dynamically_requested_fixture():
pass
@pytest.fixture()
def dependent_fixture(request):
request.getfixturevalue('dynamically_requested_fixture')
def test_dyn(dependent_fixture):
pass
"""
)
result = pytester.runpytest("--setup-only", p)
assert result.ret == 0
result.stdout.fnmatch_lines(
[
"*SETUP F dynamically_requested_fixture",
"*TEARDOWN F dynamically_requested_fixture",
]
)
def test_capturing(pytester: Pytester) -> None:
p = pytester.makepyfile(
"""
import pytest, sys
@pytest.fixture()
def one():
sys.stdout.write('this should be captured')
sys.stderr.write('this should also be captured')
@pytest.fixture()
def two(one):
assert 0
def test_capturing(two):
pass
"""
)
result = pytester.runpytest("--setup-only", p)
result.stdout.fnmatch_lines(
["this should be captured", "this should also be captured"]
)
def test_show_fixtures_and_execute_test(pytester: Pytester) -> None:
"""Verify that setups are shown and tests are executed."""
p = pytester.makepyfile(
"""
import pytest
@pytest.fixture
def arg():
assert True
def test_arg(arg):
assert False
"""
)
result = pytester.runpytest("--setup-show", p)
assert result.ret == 1
result.stdout.fnmatch_lines(
["*SETUP F arg*", "*test_arg (fixtures used: arg)F*", "*TEARDOWN F arg*"]
)
def test_setup_show_with_KeyboardInterrupt_in_test(pytester: Pytester) -> None:
p = pytester.makepyfile(
"""
import pytest
@pytest.fixture
def arg():
pass
def test_arg(arg):
raise KeyboardInterrupt()
"""
)
result = pytester.runpytest("--setup-show", p, no_reraise_ctrlc=True)
result.stdout.fnmatch_lines(
[
"*SETUP F arg*",
"*test_arg (fixtures used: arg)*",
"*TEARDOWN F arg*",
"*! KeyboardInterrupt !*",
"*= no tests ran in *",
]
)
assert result.ret == ExitCode.INTERRUPTED
def test_show_fixture_action_with_bytes(pytester: Pytester) -> None:
# Issue 7126, BytesWarning when using --setup-show with bytes parameter
test_file = pytester.makepyfile(
"""
import pytest
@pytest.mark.parametrize('data', [b'Hello World'])
def test_data(data):
pass
"""
)
result = pytester.run(
sys.executable, "-bb", "-m", "pytest", "--setup-show", str(test_file)
)
assert result.ret == 0
|