| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from enum import Enum |
| 4 |
import json |
| 5 |
import sys |
| 6 |
from typing import Literal |
| 7 |
|
| 8 |
from _pytest._io.saferepr import saferepr |
| 9 |
from _pytest.subtests import SubtestContext |
| 10 |
from _pytest.subtests import SubtestReport |
| 11 |
import pytest |
| 12 |
|
| 13 |
|
| 14 |
IS_PY311 = sys.version_info[:2] >= (3, 11) |
| 15 |
|
| 16 |
|
| 17 |
def test_failures(pytester: pytest.Pytester, monkeypatch: pytest.MonkeyPatch) -> None: |
| 18 |
monkeypatch.setenv("COLUMNS", "120") |
| 19 |
pytester.makepyfile( |
| 20 |
""" |
| 21 |
def test_foo(subtests): |
| 22 |
with subtests.test("foo subtest"): |
| 23 |
assert False, "foo subtest failure" |
| 24 |
|
| 25 |
def test_bar(subtests): |
| 26 |
with subtests.test("bar subtest"): |
| 27 |
assert False, "bar subtest failure" |
| 28 |
assert False, "test_bar also failed" |
| 29 |
|
| 30 |
def test_zaz(subtests): |
| 31 |
with subtests.test("zaz subtest"): |
| 32 |
pass |
| 33 |
""" |
| 34 |
) |
| 35 |
summary_lines = [ |
| 36 |
"*=== FAILURES ===*", |
| 37 |
# |
| 38 |
"*___ test_foo [[]foo subtest[]] ___*", |
| 39 |
"*AssertionError: foo subtest failure", |
| 40 |
# |
| 41 |
"*___ test_foo ___*", |
| 42 |
"contains 1 failed subtest", |
| 43 |
# |
| 44 |
"*___ test_bar [[]bar subtest[]] ___*", |
| 45 |
"*AssertionError: bar subtest failure", |
| 46 |
# |
| 47 |
"*___ test_bar ___*", |
| 48 |
"*AssertionError: test_bar also failed", |
| 49 |
# |
| 50 |
"*=== short test summary info ===*", |
| 51 |
"SUBFAILED[[]foo subtest[]] test_*.py::test_foo - AssertionError*", |
| 52 |
"FAILED test_*.py::test_foo - contains 1 failed subtest", |
| 53 |
"SUBFAILED[[]bar subtest[]] test_*.py::test_bar - AssertionError*", |
| 54 |
"FAILED test_*.py::test_bar - AssertionError*", |
| 55 |
] |
| 56 |
result = pytester.runpytest() |
| 57 |
result.stdout.fnmatch_lines( |
| 58 |
[ |
| 59 |
"test_*.py uFuF. * [[]100%[]]", |
| 60 |
*summary_lines, |
| 61 |
"* 4 failed, 1 passed in *", |
| 62 |
] |
| 63 |
) |
| 64 |
|
| 65 |
result = pytester.runpytest("-v") |
| 66 |
result.stdout.fnmatch_lines( |
| 67 |
[ |
| 68 |
"test_*.py::test_foo SUBFAILED[[]foo subtest[]] * [[] 33%[]]", |
| 69 |
"test_*.py::test_foo FAILED * [[] 33%[]]", |
| 70 |
"test_*.py::test_bar SUBFAILED[[]bar subtest[]] * [[] 66%[]]", |
| 71 |
"test_*.py::test_bar FAILED * [[] 66%[]]", |
| 72 |
"test_*.py::test_zaz SUBPASSED[[]zaz subtest[]] * [[]100%[]]", |
| 73 |
"test_*.py::test_zaz PASSED * [[]100%[]]", |
| 74 |
*summary_lines, |
| 75 |
"* 4 failed, 1 passed, 1 subtests passed in *", |
| 76 |
] |
| 77 |
) |
| 78 |
pytester.makeini( |
| 79 |
""" |
| 80 |
[pytest] |
| 81 |
verbosity_subtests = 0 |
| 82 |
""" |
| 83 |
) |
| 84 |
result = pytester.runpytest("-v") |
| 85 |
result.stdout.fnmatch_lines( |
| 86 |
[ |
| 87 |
"test_*.py::test_foo SUBFAILED[[]foo subtest[]] * [[] 33%[]]", |
| 88 |
"test_*.py::test_foo FAILED * [[] 33%[]]", |
| 89 |
"test_*.py::test_bar SUBFAILED[[]bar subtest[]] * [[] 66%[]]", |
| 90 |
"test_*.py::test_bar FAILED * [[] 66%[]]", |
| 91 |
"test_*.py::test_zaz PASSED * [[]100%[]]", |
| 92 |
*summary_lines, |
| 93 |
"* 4 failed, 1 passed in *", |
| 94 |
] |
| 95 |
) |
| 96 |
result.stdout.no_fnmatch_line("test_*.py::test_zaz SUBPASSED[[]zaz subtest[]]*") |
| 97 |
|
| 98 |
|
| 99 |
def test_passes(pytester: pytest.Pytester, monkeypatch: pytest.MonkeyPatch) -> None: |
| 100 |
monkeypatch.setenv("COLUMNS", "120") |
| 101 |
pytester.makepyfile( |
| 102 |
""" |
| 103 |
def test_foo(subtests): |
| 104 |
with subtests.test("foo subtest"): |
| 105 |
pass |
| 106 |
|
| 107 |
def test_bar(subtests): |
| 108 |
with subtests.test("bar subtest"): |
| 109 |
pass |
| 110 |
""" |
| 111 |
) |
| 112 |
result = pytester.runpytest() |
| 113 |
result.stdout.fnmatch_lines( |
| 114 |
[ |
| 115 |
"test_*.py .. * [[]100%[]]", |
| 116 |
"* 2 passed in *", |
| 117 |
] |
| 118 |
) |
| 119 |
|
| 120 |
result = pytester.runpytest("-v") |
| 121 |
result.stdout.fnmatch_lines( |
| 122 |
[ |
| 123 |
"*.py::test_foo SUBPASSED[[]foo subtest[]] * [[] 50%[]]", |
| 124 |
"*.py::test_foo PASSED * [[] 50%[]]", |
| 125 |
"*.py::test_bar SUBPASSED[[]bar subtest[]] * [[]100%[]]", |
| 126 |
"*.py::test_bar PASSED * [[]100%[]]", |
| 127 |
"* 2 passed, 2 subtests passed in *", |
| 128 |
] |
| 129 |
) |
| 130 |
|
| 131 |
pytester.makeini( |
| 132 |
""" |
| 133 |
[pytest] |
| 134 |
verbosity_subtests = 0 |
| 135 |
""" |
| 136 |
) |
| 137 |
result = pytester.runpytest("-v") |
| 138 |
result.stdout.fnmatch_lines( |
| 139 |
[ |
| 140 |
"*.py::test_foo PASSED * [[] 50%[]]", |
| 141 |
"*.py::test_bar PASSED * [[]100%[]]", |
| 142 |
"* 2 passed in *", |
| 143 |
] |
| 144 |
) |
| 145 |
result.stdout.no_fnmatch_line("*.py::test_foo SUBPASSED[[]foo subtest[]]*") |
| 146 |
result.stdout.no_fnmatch_line("*.py::test_bar SUBPASSED[[]bar subtest[]]*") |
| 147 |
|
| 148 |
|
| 149 |
def test_skip(pytester: pytest.Pytester, monkeypatch: pytest.MonkeyPatch) -> None: |
| 150 |
monkeypatch.setenv("COLUMNS", "120") |
| 151 |
pytester.makepyfile( |
| 152 |
""" |
| 153 |
import pytest |
| 154 |
def test_foo(subtests): |
| 155 |
with subtests.test("foo subtest"): |
| 156 |
pytest.skip("skip foo subtest") |
| 157 |
|
| 158 |
def test_bar(subtests): |
| 159 |
with subtests.test("bar subtest"): |
| 160 |
pytest.skip("skip bar subtest") |
| 161 |
pytest.skip("skip test_bar") |
| 162 |
""" |
| 163 |
) |
| 164 |
result = pytester.runpytest("-ra") |
| 165 |
result.stdout.fnmatch_lines( |
| 166 |
[ |
| 167 |
"test_*.py .s * [[]100%[]]", |
| 168 |
"*=== short test summary info ===*", |
| 169 |
"SKIPPED [[]1[]] test_skip.py:9: skip test_bar", |
| 170 |
"* 1 passed, 1 skipped in *", |
| 171 |
] |
| 172 |
) |
| 173 |
|
| 174 |
result = pytester.runpytest("-v", "-ra") |
| 175 |
result.stdout.fnmatch_lines( |
| 176 |
[ |
| 177 |
"*.py::test_foo SUBSKIPPED[[]foo subtest[]] (skip foo subtest) * [[] 50%[]]", |
| 178 |
"*.py::test_foo PASSED * [[] 50%[]]", |
| 179 |
"*.py::test_bar SUBSKIPPED[[]bar subtest[]] (skip bar subtest) * [[]100%[]]", |
| 180 |
"*.py::test_bar SKIPPED (skip test_bar) * [[]100%[]]", |
| 181 |
"*=== short test summary info ===*", |
| 182 |
"SUBSKIPPED[[]foo subtest[]] [[]1[]] *.py:*: skip foo subtest", |
| 183 |
"SUBSKIPPED[[]foo subtest[]] [[]1[]] *.py:*: skip bar subtest", |
| 184 |
"SUBSKIPPED[[]foo subtest[]] [[]1[]] *.py:*: skip test_bar", |
| 185 |
"* 1 passed, 3 skipped in *", |
| 186 |
] |
| 187 |
) |
| 188 |
|
| 189 |
pytester.makeini( |
| 190 |
""" |
| 191 |
[pytest] |
| 192 |
verbosity_subtests = 0 |
| 193 |
""" |
| 194 |
) |
| 195 |
result = pytester.runpytest("-v", "-ra") |
| 196 |
result.stdout.fnmatch_lines( |
| 197 |
[ |
| 198 |
"*.py::test_foo PASSED * [[] 50%[]]", |
| 199 |
"*.py::test_bar SKIPPED (skip test_bar) * [[]100%[]]", |
| 200 |
"*=== short test summary info ===*", |
| 201 |
"* 1 passed, 1 skipped in *", |
| 202 |
] |
| 203 |
) |
| 204 |
result.stdout.no_fnmatch_line("*.py::test_foo SUBPASSED[[]foo subtest[]]*") |
| 205 |
result.stdout.no_fnmatch_line("*.py::test_bar SUBPASSED[[]bar subtest[]]*") |
| 206 |
result.stdout.no_fnmatch_line( |
| 207 |
"SUBSKIPPED[[]foo subtest[]] [[]1[]] *.py:*: skip foo subtest" |
| 208 |
) |
| 209 |
result.stdout.no_fnmatch_line( |
| 210 |
"SUBSKIPPED[[]foo subtest[]] [[]1[]] *.py:*: skip test_bar" |
| 211 |
) |
| 212 |
|
| 213 |
|
| 214 |
def test_xfail(pytester: pytest.Pytester, monkeypatch: pytest.MonkeyPatch) -> None: |
| 215 |
monkeypatch.setenv("COLUMNS", "120") |
| 216 |
pytester.makepyfile( |
| 217 |
""" |
| 218 |
import pytest |
| 219 |
def test_foo(subtests): |
| 220 |
with subtests.test("foo subtest"): |
| 221 |
pytest.xfail("xfail foo subtest") |
| 222 |
|
| 223 |
def test_bar(subtests): |
| 224 |
with subtests.test("bar subtest"): |
| 225 |
pytest.xfail("xfail bar subtest") |
| 226 |
pytest.xfail("xfail test_bar") |
| 227 |
""" |
| 228 |
) |
| 229 |
result = pytester.runpytest("-ra") |
| 230 |
result.stdout.fnmatch_lines( |
| 231 |
[ |
| 232 |
"test_*.py .x * [[]100%[]]", |
| 233 |
"*=== short test summary info ===*", |
| 234 |
"* 1 passed, 1 xfailed in *", |
| 235 |
] |
| 236 |
) |
| 237 |
|
| 238 |
result = pytester.runpytest("-v", "-ra") |
| 239 |
result.stdout.fnmatch_lines( |
| 240 |
[ |
| 241 |
"*.py::test_foo SUBXFAIL[[]foo subtest[]] (xfail foo subtest) * [[] 50%[]]", |
| 242 |
"*.py::test_foo PASSED * [[] 50%[]]", |
| 243 |
"*.py::test_bar SUBXFAIL[[]bar subtest[]] (xfail bar subtest) * [[]100%[]]", |
| 244 |
"*.py::test_bar XFAIL (xfail test_bar) * [[]100%[]]", |
| 245 |
"*=== short test summary info ===*", |
| 246 |
"SUBXFAIL[[]foo subtest[]] *.py::test_foo - xfail foo subtest", |
| 247 |
"SUBXFAIL[[]bar subtest[]] *.py::test_bar - xfail bar subtest", |
| 248 |
"XFAIL *.py::test_bar - xfail test_bar", |
| 249 |
"* 1 passed, 3 xfailed in *", |
| 250 |
] |
| 251 |
) |
| 252 |
|
| 253 |
pytester.makeini( |
| 254 |
""" |
| 255 |
[pytest] |
| 256 |
verbosity_subtests = 0 |
| 257 |
""" |
| 258 |
) |
| 259 |
result = pytester.runpytest("-v", "-ra") |
| 260 |
result.stdout.fnmatch_lines( |
| 261 |
[ |
| 262 |
"*.py::test_foo PASSED * [[] 50%[]]", |
| 263 |
"*.py::test_bar XFAIL (xfail test_bar) * [[]100%[]]", |
| 264 |
"*=== short test summary info ===*", |
| 265 |
"* 1 passed, 1 xfailed in *", |
| 266 |
] |
| 267 |
) |
| 268 |
result.stdout.no_fnmatch_line( |
| 269 |
"SUBXFAIL[[]foo subtest[]] *.py::test_foo - xfail foo subtest" |
| 270 |
) |
| 271 |
result.stdout.no_fnmatch_line( |
| 272 |
"SUBXFAIL[[]bar subtest[]] *.py::test_bar - xfail bar subtest" |
| 273 |
) |
| 274 |
|
| 275 |
|
| 276 |
def test_typing_exported(pytester: pytest.Pytester) -> None: |
| 277 |
pytester.makepyfile( |
| 278 |
""" |
| 279 |
from pytest import Subtests |
| 280 |
|
| 281 |
def test_typing_exported(subtests: Subtests) -> None: |
| 282 |
assert isinstance(subtests, Subtests) |
| 283 |
""" |
| 284 |
) |
| 285 |
result = pytester.runpytest() |
| 286 |
result.stdout.fnmatch_lines(["*1 passed*"]) |
| 287 |
|
| 288 |
|
| 289 |
def test_subtests_and_parametrization( |
| 290 |
pytester: pytest.Pytester, monkeypatch: pytest.MonkeyPatch |
| 291 |
) -> None: |
| 292 |
monkeypatch.setenv("COLUMNS", "120") |
| 293 |
pytester.makepyfile( |
| 294 |
""" |
| 295 |
import pytest |
| 296 |
|
| 297 |
@pytest.mark.parametrize("x", [0, 1]) |
| 298 |
def test_foo(subtests, x): |
| 299 |
for i in range(3): |
| 300 |
with subtests.test("custom", i=i): |
| 301 |
assert i % 2 == 0 |
| 302 |
assert x == 0 |
| 303 |
""" |
| 304 |
) |
| 305 |
result = pytester.runpytest("-v") |
| 306 |
result.stdout.fnmatch_lines( |
| 307 |
[ |
| 308 |
"*.py::test_foo[[]0[]] SUBFAILED[[]custom[]] (i=1) *[[] 50%[]]", |
| 309 |
"*.py::test_foo[[]0[]] FAILED *[[] 50%[]]", |
| 310 |
"*.py::test_foo[[]1[]] SUBFAILED[[]custom[]] (i=1) *[[]100%[]]", |
| 311 |
"*.py::test_foo[[]1[]] FAILED *[[]100%[]]", |
| 312 |
"contains 1 failed subtest", |
| 313 |
"* 4 failed, 4 subtests passed in *", |
| 314 |
] |
| 315 |
) |
| 316 |
|
| 317 |
pytester.makeini( |
| 318 |
""" |
| 319 |
[pytest] |
| 320 |
verbosity_subtests = 0 |
| 321 |
""" |
| 322 |
) |
| 323 |
result = pytester.runpytest("-v") |
| 324 |
result.stdout.fnmatch_lines( |
| 325 |
[ |
| 326 |
"*.py::test_foo[[]0[]] SUBFAILED[[]custom[]] (i=1) *[[] 50%[]]", |
| 327 |
"*.py::test_foo[[]0[]] FAILED *[[] 50%[]]", |
| 328 |
"*.py::test_foo[[]1[]] SUBFAILED[[]custom[]] (i=1) *[[]100%[]]", |
| 329 |
"*.py::test_foo[[]1[]] FAILED *[[]100%[]]", |
| 330 |
"contains 1 failed subtest", |
| 331 |
"* 4 failed in *", |
| 332 |
] |
| 333 |
) |
| 334 |
|
| 335 |
|
| 336 |
def test_subtests_fail_top_level_test(pytester: pytest.Pytester) -> None: |
| 337 |
pytester.makepyfile( |
| 338 |
""" |
| 339 |
import pytest |
| 340 |
|
| 341 |
def test_foo(subtests): |
| 342 |
for i in range(3): |
| 343 |
with subtests.test("custom", i=i): |
| 344 |
assert i % 2 == 0 |
| 345 |
""" |
| 346 |
) |
| 347 |
result = pytester.runpytest("-v") |
| 348 |
result.stdout.fnmatch_lines( |
| 349 |
[ |
| 350 |
"* 2 failed, 2 subtests passed in *", |
| 351 |
] |
| 352 |
) |
| 353 |
|
| 354 |
|
| 355 |
def test_subtests_do_not_overwrite_top_level_failure(pytester: pytest.Pytester) -> None: |
| 356 |
pytester.makepyfile( |
| 357 |
""" |
| 358 |
import pytest |
| 359 |
|
| 360 |
def test_foo(subtests): |
| 361 |
for i in range(3): |
| 362 |
with subtests.test("custom", i=i): |
| 363 |
assert i % 2 == 0 |
| 364 |
assert False, "top-level failure" |
| 365 |
""" |
| 366 |
) |
| 367 |
result = pytester.runpytest("-v") |
| 368 |
result.stdout.fnmatch_lines( |
| 369 |
[ |
| 370 |
"*AssertionError: top-level failure", |
| 371 |
"* 2 failed, 2 subtests passed in *", |
| 372 |
] |
| 373 |
) |
| 374 |
|
| 375 |
|
| 376 |
def test_msg_not_a_string( |
| 377 |
pytester: pytest.Pytester, monkeypatch: pytest.MonkeyPatch |
| 378 |
) -> None: |
| 379 |
""" |
| 380 |
Using a non-string in subtests.test() should still show it in the terminal (#14195). |
| 381 |
|
| 382 |
Note: this was not a problem originally with the subtests fixture, only with TestCase.subTest; this test |
| 383 |
was added for symmetry. |
| 384 |
""" |
| 385 |
monkeypatch.setenv("COLUMNS", "120") |
| 386 |
pytester.makepyfile( |
| 387 |
""" |
| 388 |
def test_int_msg(subtests): |
| 389 |
with subtests.test(42): |
| 390 |
assert False, "subtest failure" |
| 391 |
|
| 392 |
def test_no_msg(subtests): |
| 393 |
with subtests.test(): |
| 394 |
assert False, "subtest failure" |
| 395 |
""" |
| 396 |
) |
| 397 |
result = pytester.runpytest() |
| 398 |
result.stdout.fnmatch_lines( |
| 399 |
[ |
| 400 |
"SUBFAILED[[]42[]] test_msg_not_a_string.py::test_int_msg - AssertionError: subtest failure", |
| 401 |
"SUBFAILED(<subtest>) test_msg_not_a_string.py::test_no_msg - AssertionError: subtest failure", |
| 402 |
] |
| 403 |
) |
| 404 |
|
| 405 |
|
| 406 |
@pytest.mark.parametrize("flag", ["--last-failed", "--stepwise"]) |
| 407 |
def test_subtests_last_failed_step_wise(pytester: pytest.Pytester, flag: str) -> None: |
| 408 |
"""Check that --last-failed and --step-wise correctly rerun tests with failed subtests.""" |
| 409 |
pytester.makepyfile( |
| 410 |
""" |
| 411 |
import pytest |
| 412 |
|
| 413 |
def test_foo(subtests): |
| 414 |
for i in range(3): |
| 415 |
with subtests.test("custom", i=i): |
| 416 |
assert i % 2 == 0 |
| 417 |
""" |
| 418 |
) |
| 419 |
result = pytester.runpytest("-v") |
| 420 |
result.stdout.fnmatch_lines( |
| 421 |
[ |
| 422 |
"* 2 failed, 2 subtests passed in *", |
| 423 |
] |
| 424 |
) |
| 425 |
|
| 426 |
result = pytester.runpytest("-v", flag) |
| 427 |
result.stdout.fnmatch_lines( |
| 428 |
[ |
| 429 |
"* 2 failed, 2 subtests passed in *", |
| 430 |
] |
| 431 |
) |
| 432 |
|
| 433 |
|
| 434 |
class TestUnittestSubTest: |
| 435 |
"""Test unittest.TestCase.subTest functionality.""" |
| 436 |
|
| 437 |
def test_failures( |
| 438 |
self, pytester: pytest.Pytester, monkeypatch: pytest.MonkeyPatch |
| 439 |
) -> None: |
| 440 |
monkeypatch.setenv("COLUMNS", "120") |
| 441 |
pytester.makepyfile( |
| 442 |
""" |
| 443 |
from unittest import TestCase |
| 444 |
|
| 445 |
class T(TestCase): |
| 446 |
def test_foo(self): |
| 447 |
with self.subTest("foo subtest"): |
| 448 |
assert False, "foo subtest failure" |
| 449 |
|
| 450 |
def test_bar(self): |
| 451 |
with self.subTest("bar subtest"): |
| 452 |
assert False, "bar subtest failure" |
| 453 |
assert False, "test_bar also failed" |
| 454 |
|
| 455 |
def test_zaz(self): |
| 456 |
with self.subTest("zaz subtest"): |
| 457 |
pass |
| 458 |
""" |
| 459 |
) |
| 460 |
result = pytester.runpytest() |
| 461 |
result.stdout.fnmatch_lines( |
| 462 |
[ |
| 463 |
"* 3 failed, 2 passed in *", |
| 464 |
] |
| 465 |
) |
| 466 |
|
| 467 |
def test_passes( |
| 468 |
self, pytester: pytest.Pytester, monkeypatch: pytest.MonkeyPatch |
| 469 |
) -> None: |
| 470 |
monkeypatch.setenv("COLUMNS", "120") |
| 471 |
pytester.makepyfile( |
| 472 |
""" |
| 473 |
from unittest import TestCase |
| 474 |
|
| 475 |
class T(TestCase): |
| 476 |
def test_foo(self): |
| 477 |
with self.subTest("foo subtest"): |
| 478 |
pass |
| 479 |
|
| 480 |
def test_bar(self): |
| 481 |
with self.subTest("bar subtest"): |
| 482 |
pass |
| 483 |
|
| 484 |
def test_zaz(self): |
| 485 |
with self.subTest("zaz subtest"): |
| 486 |
pass |
| 487 |
""" |
| 488 |
) |
| 489 |
result = pytester.runpytest() |
| 490 |
result.stdout.fnmatch_lines( |
| 491 |
[ |
| 492 |
"* 3 passed in *", |
| 493 |
] |
| 494 |
) |
| 495 |
|
| 496 |
def test_skip( |
| 497 |
self, |
| 498 |
pytester: pytest.Pytester, |
| 499 |
) -> None: |
| 500 |
pytester.makepyfile( |
| 501 |
""" |
| 502 |
from unittest import TestCase, main |
| 503 |
|
| 504 |
class T(TestCase): |
| 505 |
|
| 506 |
def test_foo(self): |
| 507 |
for i in range(5): |
| 508 |
with self.subTest(msg="custom", i=i): |
| 509 |
if i % 2 == 0: |
| 510 |
self.skipTest('even number') |
| 511 |
""" |
| 512 |
) |
| 513 |
# This output might change #13756. |
| 514 |
result = pytester.runpytest() |
| 515 |
result.stdout.fnmatch_lines(["* 1 passed in *"]) |
| 516 |
|
| 517 |
def test_non_subtest_skip( |
| 518 |
self, pytester: pytest.Pytester, monkeypatch: pytest.MonkeyPatch |
| 519 |
) -> None: |
| 520 |
monkeypatch.setenv("COLUMNS", "120") |
| 521 |
pytester.makepyfile( |
| 522 |
""" |
| 523 |
from unittest import TestCase, main |
| 524 |
|
| 525 |
class T(TestCase): |
| 526 |
|
| 527 |
def test_foo(self): |
| 528 |
with self.subTest(msg="subtest"): |
| 529 |
assert False, "failed subtest" |
| 530 |
self.skipTest('non-subtest skip') |
| 531 |
""" |
| 532 |
) |
| 533 |
# This output might change #13756. |
| 534 |
result = pytester.runpytest() |
| 535 |
result.stdout.fnmatch_lines( |
| 536 |
[ |
| 537 |
"SUBFAILED[[]subtest[]] test_non_subtest_skip.py::T::test_foo*", |
| 538 |
"* 1 failed, 1 skipped in *", |
| 539 |
] |
| 540 |
) |
| 541 |
|
| 542 |
def test_xfail( |
| 543 |
self, |
| 544 |
pytester: pytest.Pytester, |
| 545 |
) -> None: |
| 546 |
pytester.makepyfile( |
| 547 |
""" |
| 548 |
import pytest |
| 549 |
from unittest import expectedFailure, TestCase |
| 550 |
|
| 551 |
class T(TestCase): |
| 552 |
@expectedFailure |
| 553 |
def test_foo(self): |
| 554 |
for i in range(5): |
| 555 |
with self.subTest(msg="custom", i=i): |
| 556 |
if i % 2 == 0: |
| 557 |
raise pytest.xfail('even number') |
| 558 |
|
| 559 |
if __name__ == '__main__': |
| 560 |
main() |
| 561 |
""" |
| 562 |
) |
| 563 |
# This output might change #13756. |
| 564 |
result = pytester.runpytest() |
| 565 |
result.stdout.fnmatch_lines(["* 1 xfailed in *"]) |
| 566 |
|
| 567 |
def test_only_original_skip_is_called( |
| 568 |
self, |
| 569 |
pytester: pytest.Pytester, |
| 570 |
monkeypatch: pytest.MonkeyPatch, |
| 571 |
) -> None: |
| 572 |
"""Regression test for pytest-dev/pytest-subtests#173.""" |
| 573 |
monkeypatch.setenv("COLUMNS", "120") |
| 574 |
pytester.makepyfile( |
| 575 |
""" |
| 576 |
import unittest |
| 577 |
from unittest import TestCase |
| 578 |
|
| 579 |
@unittest.skip("skip this test") |
| 580 |
class T(unittest.TestCase): |
| 581 |
def test_foo(self): |
| 582 |
assert 1 == 2 |
| 583 |
""" |
| 584 |
) |
| 585 |
result = pytester.runpytest("-v", "-rsf") |
| 586 |
result.stdout.fnmatch_lines( |
| 587 |
["SKIPPED [1] test_only_original_skip_is_called.py:6: skip this test"] |
| 588 |
) |
| 589 |
|
| 590 |
def test_skip_with_failure( |
| 591 |
self, |
| 592 |
pytester: pytest.Pytester, |
| 593 |
monkeypatch: pytest.MonkeyPatch, |
| 594 |
) -> None: |
| 595 |
monkeypatch.setenv("COLUMNS", "120") |
| 596 |
pytester.makepyfile( |
| 597 |
""" |
| 598 |
import pytest |
| 599 |
from unittest import TestCase |
| 600 |
|
| 601 |
class T(TestCase): |
| 602 |
def test_foo(self): |
| 603 |
with self.subTest("subtest 1"): |
| 604 |
self.skipTest(f"skip subtest 1") |
| 605 |
with self.subTest("subtest 2"): |
| 606 |
assert False, "fail subtest 2" |
| 607 |
""" |
| 608 |
) |
| 609 |
|
| 610 |
result = pytester.runpytest("-ra") |
| 611 |
result.stdout.fnmatch_lines( |
| 612 |
[ |
| 613 |
"*.py u. * [[]100%[]]", |
| 614 |
"*=== short test summary info ===*", |
| 615 |
"SUBFAILED[[]subtest 2[]] *.py::T::test_foo - AssertionError: fail subtest 2", |
| 616 |
"* 1 failed, 1 passed in *", |
| 617 |
] |
| 618 |
) |
| 619 |
|
| 620 |
result = pytester.runpytest("-v", "-ra") |
| 621 |
result.stdout.fnmatch_lines( |
| 622 |
[ |
| 623 |
"*.py::T::test_foo SUBSKIPPED[[]subtest 1[]] (skip subtest 1) * [[]100%[]]", |
| 624 |
"*.py::T::test_foo SUBFAILED[[]subtest 2[]] * [[]100%[]]", |
| 625 |
"*.py::T::test_foo PASSED * [[]100%[]]", |
| 626 |
"SUBSKIPPED[[]subtest 1[]] [[]1[]] *.py:*: skip subtest 1", |
| 627 |
"SUBFAILED[[]subtest 2[]] *.py::T::test_foo - AssertionError: fail subtest 2", |
| 628 |
"* 1 failed, 1 passed, 1 skipped in *", |
| 629 |
] |
| 630 |
) |
| 631 |
|
| 632 |
pytester.makeini( |
| 633 |
""" |
| 634 |
[pytest] |
| 635 |
verbosity_subtests = 0 |
| 636 |
""" |
| 637 |
) |
| 638 |
result = pytester.runpytest("-v", "-ra") |
| 639 |
result.stdout.fnmatch_lines( |
| 640 |
[ |
| 641 |
"*.py::T::test_foo SUBFAILED[[]subtest 2[]] * [[]100%[]]", |
| 642 |
"*.py::T::test_foo PASSED * [[]100%[]]", |
| 643 |
"*=== short test summary info ===*", |
| 644 |
r"SUBFAILED[[]subtest 2[]] *.py::T::test_foo - AssertionError: fail subtest 2", |
| 645 |
r"* 1 failed, 1 passed in *", |
| 646 |
] |
| 647 |
) |
| 648 |
result.stdout.no_fnmatch_line( |
| 649 |
"*.py::T::test_foo SUBSKIPPED[[]subtest 1[]] (skip subtest 1) * [[]100%[]]" |
| 650 |
) |
| 651 |
result.stdout.no_fnmatch_line( |
| 652 |
"SUBSKIPPED[[]subtest 1[]] [[]1[]] *.py:*: skip subtest 1" |
| 653 |
) |
| 654 |
|
| 655 |
def test_msg_not_a_string( |
| 656 |
self, pytester: pytest.Pytester, monkeypatch: pytest.MonkeyPatch |
| 657 |
) -> None: |
| 658 |
"""Using a non-string in TestCase.subTest should still show it in the terminal (#14195).""" |
| 659 |
monkeypatch.setenv("COLUMNS", "120") |
| 660 |
pytester.makepyfile( |
| 661 |
""" |
| 662 |
from unittest import TestCase |
| 663 |
|
| 664 |
class T(TestCase): |
| 665 |
def test_int_msg(self): |
| 666 |
with self.subTest(42): |
| 667 |
assert False, "subtest failure" |
| 668 |
|
| 669 |
def test_no_msg(self): |
| 670 |
with self.subTest(): |
| 671 |
assert False, "subtest failure" |
| 672 |
""" |
| 673 |
) |
| 674 |
result = pytester.runpytest() |
| 675 |
result.stdout.fnmatch_lines( |
| 676 |
[ |
| 677 |
"SUBFAILED[[]42[]] test_msg_not_a_string.py::T::test_int_msg - AssertionError: subtest failure", |
| 678 |
"SUBFAILED(<subtest>) test_msg_not_a_string.py::T::test_no_msg - AssertionError: subtest failure", |
| 679 |
] |
| 680 |
) |
| 681 |
|
| 682 |
|
| 683 |
class TestCapture: |
| 684 |
def create_file(self, pytester: pytest.Pytester) -> None: |
| 685 |
pytester.makepyfile( |
| 686 |
""" |
| 687 |
import sys |
| 688 |
def test(subtests): |
| 689 |
print() |
| 690 |
print('start test') |
| 691 |
|
| 692 |
with subtests.test(i='A'): |
| 693 |
print("hello stdout A") |
| 694 |
print("hello stderr A", file=sys.stderr) |
| 695 |
assert 0 |
| 696 |
|
| 697 |
with subtests.test(i='B'): |
| 698 |
print("hello stdout B") |
| 699 |
print("hello stderr B", file=sys.stderr) |
| 700 |
assert 0 |
| 701 |
|
| 702 |
print('end test') |
| 703 |
assert 0 |
| 704 |
""" |
| 705 |
) |
| 706 |
|
| 707 |
@pytest.mark.parametrize("mode", ["fd", "sys"]) |
| 708 |
def test_capturing(self, pytester: pytest.Pytester, mode: str) -> None: |
| 709 |
self.create_file(pytester) |
| 710 |
result = pytester.runpytest(f"--capture={mode}") |
| 711 |
result.stdout.fnmatch_lines( |
| 712 |
[ |
| 713 |
"*__ test (i='A') __*", |
| 714 |
"*Captured stdout call*", |
| 715 |
"hello stdout A", |
| 716 |
"*Captured stderr call*", |
| 717 |
"hello stderr A", |
| 718 |
"*__ test (i='B') __*", |
| 719 |
"*Captured stdout call*", |
| 720 |
"hello stdout B", |
| 721 |
"*Captured stderr call*", |
| 722 |
"hello stderr B", |
| 723 |
"*__ test __*", |
| 724 |
"*Captured stdout call*", |
| 725 |
"start test", |
| 726 |
"end test", |
| 727 |
] |
| 728 |
) |
| 729 |
|
| 730 |
def test_no_capture(self, pytester: pytest.Pytester) -> None: |
| 731 |
self.create_file(pytester) |
| 732 |
result = pytester.runpytest("-s") |
| 733 |
result.stdout.fnmatch_lines( |
| 734 |
[ |
| 735 |
"start test", |
| 736 |
"hello stdout A", |
| 737 |
"uhello stdout B", |
| 738 |
"uend test", |
| 739 |
"*__ test (i='A') __*", |
| 740 |
"*__ test (i='B') __*", |
| 741 |
"*__ test __*", |
| 742 |
] |
| 743 |
) |
| 744 |
result.stderr.fnmatch_lines(["hello stderr A", "hello stderr B"]) |
| 745 |
|
| 746 |
@pytest.mark.parametrize("fixture", ["capsys", "capfd"]) |
| 747 |
def test_capture_with_fixture( |
| 748 |
self, pytester: pytest.Pytester, fixture: Literal["capsys", "capfd"] |
| 749 |
) -> None: |
| 750 |
pytester.makepyfile( |
| 751 |
rf""" |
| 752 |
import sys |
| 753 |
|
| 754 |
def test(subtests, {fixture}): |
| 755 |
print('start test') |
| 756 |
|
| 757 |
with subtests.test(i='A'): |
| 758 |
print("hello stdout A") |
| 759 |
print("hello stderr A", file=sys.stderr) |
| 760 |
|
| 761 |
out, err = {fixture}.readouterr() |
| 762 |
assert out == 'start test\nhello stdout A\n' |
| 763 |
assert err == 'hello stderr A\n' |
| 764 |
""" |
| 765 |
) |
| 766 |
result = pytester.runpytest() |
| 767 |
result.stdout.fnmatch_lines( |
| 768 |
[ |
| 769 |
"*1 passed*", |
| 770 |
] |
| 771 |
) |
| 772 |
|
| 773 |
|
| 774 |
class TestLogging: |
| 775 |
def create_file(self, pytester: pytest.Pytester) -> None: |
| 776 |
pytester.makepyfile( |
| 777 |
""" |
| 778 |
import logging |
| 779 |
|
| 780 |
def test_foo(subtests): |
| 781 |
logging.info("before") |
| 782 |
|
| 783 |
with subtests.test("sub1"): |
| 784 |
print("sub1 stdout") |
| 785 |
logging.info("sub1 logging") |
| 786 |
logging.debug("sub1 logging debug") |
| 787 |
|
| 788 |
with subtests.test("sub2"): |
| 789 |
print("sub2 stdout") |
| 790 |
logging.info("sub2 logging") |
| 791 |
logging.debug("sub2 logging debug") |
| 792 |
assert False |
| 793 |
""" |
| 794 |
) |
| 795 |
|
| 796 |
def test_capturing_info(self, pytester: pytest.Pytester) -> None: |
| 797 |
self.create_file(pytester) |
| 798 |
result = pytester.runpytest("--log-level=INFO") |
| 799 |
result.stdout.fnmatch_lines( |
| 800 |
[ |
| 801 |
"*___ test_foo [[]sub2[]] __*", |
| 802 |
"*-- Captured stdout call --*", |
| 803 |
"sub2 stdout", |
| 804 |
"*-- Captured log call ---*", |
| 805 |
"INFO * before", |
| 806 |
"INFO * sub1 logging", |
| 807 |
"INFO * sub2 logging", |
| 808 |
"*== short test summary info ==*", |
| 809 |
] |
| 810 |
) |
| 811 |
result.stdout.no_fnmatch_line("sub1 logging debug") |
| 812 |
result.stdout.no_fnmatch_line("sub2 logging debug") |
| 813 |
|
| 814 |
def test_capturing_debug(self, pytester: pytest.Pytester) -> None: |
| 815 |
self.create_file(pytester) |
| 816 |
result = pytester.runpytest("--log-level=DEBUG") |
| 817 |
result.stdout.fnmatch_lines( |
| 818 |
[ |
| 819 |
"*___ test_foo [[]sub2[]] __*", |
| 820 |
"*-- Captured stdout call --*", |
| 821 |
"sub2 stdout", |
| 822 |
"*-- Captured log call ---*", |
| 823 |
"INFO * before", |
| 824 |
"INFO * sub1 logging", |
| 825 |
"DEBUG * sub1 logging debug", |
| 826 |
"INFO * sub2 logging", |
| 827 |
"DEBUG * sub2 logging debug", |
| 828 |
"*== short test summary info ==*", |
| 829 |
] |
| 830 |
) |
| 831 |
|
| 832 |
def test_caplog(self, pytester: pytest.Pytester) -> None: |
| 833 |
pytester.makepyfile( |
| 834 |
""" |
| 835 |
import logging |
| 836 |
|
| 837 |
def test(subtests, caplog): |
| 838 |
caplog.set_level(logging.INFO) |
| 839 |
logging.info("start test") |
| 840 |
|
| 841 |
with subtests.test("sub1"): |
| 842 |
logging.info("inside %s", "subtest1") |
| 843 |
|
| 844 |
assert len(caplog.records) == 2 |
| 845 |
assert caplog.records[0].getMessage() == "start test" |
| 846 |
assert caplog.records[1].getMessage() == "inside subtest1" |
| 847 |
""" |
| 848 |
) |
| 849 |
result = pytester.runpytest() |
| 850 |
result.stdout.fnmatch_lines( |
| 851 |
[ |
| 852 |
"*1 passed*", |
| 853 |
] |
| 854 |
) |
| 855 |
|
| 856 |
def test_no_logging(self, pytester: pytest.Pytester) -> None: |
| 857 |
pytester.makepyfile( |
| 858 |
""" |
| 859 |
import logging |
| 860 |
|
| 861 |
def test(subtests): |
| 862 |
logging.info("start log line") |
| 863 |
|
| 864 |
with subtests.test("sub passing"): |
| 865 |
logging.info("inside %s", "passing log line") |
| 866 |
|
| 867 |
with subtests.test("sub failing"): |
| 868 |
logging.info("inside %s", "failing log line") |
| 869 |
assert False |
| 870 |
|
| 871 |
logging.info("end log line") |
| 872 |
""" |
| 873 |
) |
| 874 |
result = pytester.runpytest("-p no:logging") |
| 875 |
result.stdout.fnmatch_lines( |
| 876 |
[ |
| 877 |
"*2 failed in*", |
| 878 |
] |
| 879 |
) |
| 880 |
result.stdout.no_fnmatch_line("*root:test_no_logging.py*log line*") |
| 881 |
|
| 882 |
|
| 883 |
class TestDebugging: |
| 884 |
"""Check --pdb support for subtests fixture and TestCase.subTest.""" |
| 885 |
|
| 886 |
class _FakePdb: |
| 887 |
"""Fake debugger class implementation that tracks which methods were called on it.""" |
| 888 |
|
| 889 |
quitting: bool = False |
| 890 |
calls: list[str] = [] |
| 891 |
|
| 892 |
def __init__(self, *_: object, **__: object) -> None: |
| 893 |
self.calls.append("init") |
| 894 |
|
| 895 |
def reset(self) -> None: |
| 896 |
self.calls.append("reset") |
| 897 |
|
| 898 |
def interaction(self, *_: object) -> None: |
| 899 |
self.calls.append("interaction") |
| 900 |
|
| 901 |
@pytest.fixture(autouse=True) |
| 902 |
def cleanup_calls(self) -> None: |
| 903 |
self._FakePdb.calls.clear() |
| 904 |
|
| 905 |
def test_pdb_fixture( |
| 906 |
self, pytester: pytest.Pytester, monkeypatch: pytest.MonkeyPatch |
| 907 |
) -> None: |
| 908 |
pytester.makepyfile( |
| 909 |
""" |
| 910 |
def test(subtests): |
| 911 |
with subtests.test(): |
| 912 |
assert 0 |
| 913 |
""" |
| 914 |
) |
| 915 |
self.runpytest_and_check_pdb(pytester, monkeypatch) |
| 916 |
|
| 917 |
def test_pdb_unittest( |
| 918 |
self, pytester: pytest.Pytester, monkeypatch: pytest.MonkeyPatch |
| 919 |
) -> None: |
| 920 |
pytester.makepyfile( |
| 921 |
""" |
| 922 |
from unittest import TestCase |
| 923 |
class Test(TestCase): |
| 924 |
def test(self): |
| 925 |
with self.subTest(): |
| 926 |
assert 0 |
| 927 |
""" |
| 928 |
) |
| 929 |
self.runpytest_and_check_pdb(pytester, monkeypatch) |
| 930 |
|
| 931 |
def runpytest_and_check_pdb( |
| 932 |
self, pytester: pytest.Pytester, monkeypatch: pytest.MonkeyPatch |
| 933 |
) -> None: |
| 934 |
# Install the fake pdb implementation in _pytest.subtests so we can reference |
| 935 |
# it in the command line (any module would do). |
| 936 |
import _pytest.subtests |
| 937 |
|
| 938 |
monkeypatch.setattr( |
| 939 |
_pytest.subtests, "_CustomPdb", self._FakePdb, raising=False |
| 940 |
) |
| 941 |
result = pytester.runpytest("--pdb", "--pdbcls=_pytest.subtests:_CustomPdb") |
| 942 |
|
| 943 |
# Ensure pytest entered in debugging mode when encountering the failing |
| 944 |
# assert. |
| 945 |
result.stdout.fnmatch_lines("*entering PDB*") |
| 946 |
assert self._FakePdb.calls == ["init", "reset", "interaction"] |
| 947 |
|
| 948 |
|
| 949 |
def test_exitfirst(pytester: pytest.Pytester) -> None: |
| 950 |
"""Validate that when passing --exitfirst the test exits after the first failed subtest.""" |
| 951 |
pytester.makepyfile( |
| 952 |
""" |
| 953 |
def test_foo(subtests): |
| 954 |
with subtests.test("sub1"): |
| 955 |
assert False |
| 956 |
|
| 957 |
with subtests.test("sub2"): |
| 958 |
assert False |
| 959 |
""" |
| 960 |
) |
| 961 |
result = pytester.runpytest("--exitfirst") |
| 962 |
assert result.parseoutcomes()["failed"] == 2 |
| 963 |
result.stdout.fnmatch_lines( |
| 964 |
[ |
| 965 |
"SUBFAILED*[[]sub1[]] *.py::test_foo - assert False*", |
| 966 |
"FAILED *.py::test_foo - assert False", |
| 967 |
"* stopping after 2 failures*", |
| 968 |
], |
| 969 |
consecutive=True, |
| 970 |
) |
| 971 |
result.stdout.no_fnmatch_line("*sub2*") # sub2 not executed. |
| 972 |
|
| 973 |
|
| 974 |
def test_do_not_swallow_pytest_exit(pytester: pytest.Pytester) -> None: |
| 975 |
pytester.makepyfile( |
| 976 |
""" |
| 977 |
import pytest |
| 978 |
def test(subtests): |
| 979 |
with subtests.test(): |
| 980 |
pytest.exit() |
| 981 |
|
| 982 |
def test2(): pass |
| 983 |
""" |
| 984 |
) |
| 985 |
result = pytester.runpytest_subprocess() |
| 986 |
result.stdout.fnmatch_lines( |
| 987 |
[ |
| 988 |
"* _pytest.outcomes.Exit *", |
| 989 |
"* 1 failed in *", |
| 990 |
] |
| 991 |
) |
| 992 |
|
| 993 |
|
| 994 |
def test_nested(pytester: pytest.Pytester) -> None: |
| 995 |
""" |
| 996 |
Currently we do nothing special with nested subtests. |
| 997 |
|
| 998 |
This test only sediments how they work now, we might reconsider adding some kind of nesting support in the future. |
| 999 |
""" |
| 1000 |
pytester.makepyfile( |
| 1001 |
""" |
| 1002 |
import pytest |
| 1003 |
def test(subtests): |
| 1004 |
with subtests.test("a"): |
| 1005 |
with subtests.test("b"): |
| 1006 |
assert False, "b failed" |
| 1007 |
assert False, "a failed" |
| 1008 |
""" |
| 1009 |
) |
| 1010 |
result = pytester.runpytest_subprocess() |
| 1011 |
result.stdout.fnmatch_lines( |
| 1012 |
[ |
| 1013 |
"SUBFAILED[b] test_nested.py::test - AssertionError: b failed", |
| 1014 |
"SUBFAILED[a] test_nested.py::test - AssertionError: a failed", |
| 1015 |
"* 3 failed in *", |
| 1016 |
] |
| 1017 |
) |
| 1018 |
|
| 1019 |
|
| 1020 |
class MyEnum(Enum): |
| 1021 |
"""Used in test_serialization, needs to be declared at the module level to be pickled.""" |
| 1022 |
|
| 1023 |
A = "A" |
| 1024 |
|
| 1025 |
|
| 1026 |
def test_serialization() -> None: |
| 1027 |
"""Ensure subtest's kwargs are serialized using `saferepr` (pytest-dev/pytest-xdist#1273).""" |
| 1028 |
from _pytest.subtests import pytest_report_from_serializable |
| 1029 |
from _pytest.subtests import pytest_report_to_serializable |
| 1030 |
|
| 1031 |
report = SubtestReport( |
| 1032 |
"test_foo::test_foo", |
| 1033 |
("test_foo.py", 12, ""), |
| 1034 |
keywords={}, |
| 1035 |
outcome="passed", |
| 1036 |
when="call", |
| 1037 |
longrepr=None, |
| 1038 |
context=SubtestContext(msg="custom message", kwargs=dict(i=10, a=MyEnum.A)), |
| 1039 |
) |
| 1040 |
data = pytest_report_to_serializable(report) |
| 1041 |
assert data is not None |
| 1042 |
# Ensure the report is actually serializable to JSON. |
| 1043 |
_ = json.dumps(data) |
| 1044 |
new_report = pytest_report_from_serializable(data) |
| 1045 |
assert new_report is not None |
| 1046 |
assert new_report.context == SubtestContext( |
| 1047 |
msg="custom message", kwargs=dict(i=saferepr(10), a=saferepr(MyEnum.A)) |
| 1048 |
) |
| 1049 |
|
| 1050 |
|
| 1051 |
def test_serialization_xdist(pytester: pytest.Pytester) -> None: # pragma: no cover |
| 1052 |
"""Regression test for pytest-dev/pytest-xdist#1273.""" |
| 1053 |
pytest.importorskip("xdist") |
| 1054 |
pytester.makepyfile( |
| 1055 |
""" |
| 1056 |
from enum import Enum |
| 1057 |
import unittest |
| 1058 |
|
| 1059 |
class MyEnum(Enum): |
| 1060 |
A = "A" |
| 1061 |
|
| 1062 |
def test(subtests): |
| 1063 |
with subtests.test(a=MyEnum.A): |
| 1064 |
pass |
| 1065 |
|
| 1066 |
class T(unittest.TestCase): |
| 1067 |
|
| 1068 |
def test(self): |
| 1069 |
with self.subTest(a=MyEnum.A): |
| 1070 |
pass |
| 1071 |
""" |
| 1072 |
) |
| 1073 |
pytester.syspathinsert() |
| 1074 |
result = pytester.runpytest("-n1", "-pxdist.plugin") |
| 1075 |
result.assert_outcomes(passed=2) |