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 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977
|
from __future__ import annotations
import sys
from typing import Literal
from _pytest.subtests import SubtestContext
from _pytest.subtests import SubtestReport
import pytest
IS_PY311 = sys.version_info[:2] >= (3, 11)
def test_failures(pytester: pytest.Pytester, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setenv("COLUMNS", "120")
pytester.makepyfile(
"""
def test_foo(subtests):
with subtests.test("foo subtest"):
assert False, "foo subtest failure"
def test_bar(subtests):
with subtests.test("bar subtest"):
assert False, "bar subtest failure"
assert False, "test_bar also failed"
def test_zaz(subtests):
with subtests.test("zaz subtest"):
pass
"""
)
summary_lines = [
"*=== FAILURES ===*",
#
"*___ test_foo [[]foo subtest[]] ___*",
"*AssertionError: foo subtest failure",
#
"*___ test_foo ___*",
"contains 1 failed subtest",
#
"*___ test_bar [[]bar subtest[]] ___*",
"*AssertionError: bar subtest failure",
#
"*___ test_bar ___*",
"*AssertionError: test_bar also failed",
#
"*=== short test summary info ===*",
"SUBFAILED[[]foo subtest[]] test_*.py::test_foo - AssertionError*",
"FAILED test_*.py::test_foo - contains 1 failed subtest",
"SUBFAILED[[]bar subtest[]] test_*.py::test_bar - AssertionError*",
"FAILED test_*.py::test_bar - AssertionError*",
]
result = pytester.runpytest()
result.stdout.fnmatch_lines(
[
"test_*.py uFuF. * [[]100%[]]",
*summary_lines,
"* 4 failed, 1 passed in *",
]
)
result = pytester.runpytest("-v")
result.stdout.fnmatch_lines(
[
"test_*.py::test_foo SUBFAILED[[]foo subtest[]] * [[] 33%[]]",
"test_*.py::test_foo FAILED * [[] 33%[]]",
"test_*.py::test_bar SUBFAILED[[]bar subtest[]] * [[] 66%[]]",
"test_*.py::test_bar FAILED * [[] 66%[]]",
"test_*.py::test_zaz SUBPASSED[[]zaz subtest[]] * [[]100%[]]",
"test_*.py::test_zaz PASSED * [[]100%[]]",
*summary_lines,
"* 4 failed, 1 passed, 1 subtests passed in *",
]
)
pytester.makeini(
"""
[pytest]
verbosity_subtests = 0
"""
)
result = pytester.runpytest("-v")
result.stdout.fnmatch_lines(
[
"test_*.py::test_foo SUBFAILED[[]foo subtest[]] * [[] 33%[]]",
"test_*.py::test_foo FAILED * [[] 33%[]]",
"test_*.py::test_bar SUBFAILED[[]bar subtest[]] * [[] 66%[]]",
"test_*.py::test_bar FAILED * [[] 66%[]]",
"test_*.py::test_zaz PASSED * [[]100%[]]",
*summary_lines,
"* 4 failed, 1 passed in *",
]
)
result.stdout.no_fnmatch_line("test_*.py::test_zaz SUBPASSED[[]zaz subtest[]]*")
def test_passes(pytester: pytest.Pytester, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setenv("COLUMNS", "120")
pytester.makepyfile(
"""
def test_foo(subtests):
with subtests.test("foo subtest"):
pass
def test_bar(subtests):
with subtests.test("bar subtest"):
pass
"""
)
result = pytester.runpytest()
result.stdout.fnmatch_lines(
[
"test_*.py .. * [[]100%[]]",
"* 2 passed in *",
]
)
result = pytester.runpytest("-v")
result.stdout.fnmatch_lines(
[
"*.py::test_foo SUBPASSED[[]foo subtest[]] * [[] 50%[]]",
"*.py::test_foo PASSED * [[] 50%[]]",
"*.py::test_bar SUBPASSED[[]bar subtest[]] * [[]100%[]]",
"*.py::test_bar PASSED * [[]100%[]]",
"* 2 passed, 2 subtests passed in *",
]
)
pytester.makeini(
"""
[pytest]
verbosity_subtests = 0
"""
)
result = pytester.runpytest("-v")
result.stdout.fnmatch_lines(
[
"*.py::test_foo PASSED * [[] 50%[]]",
"*.py::test_bar PASSED * [[]100%[]]",
"* 2 passed in *",
]
)
result.stdout.no_fnmatch_line("*.py::test_foo SUBPASSED[[]foo subtest[]]*")
result.stdout.no_fnmatch_line("*.py::test_bar SUBPASSED[[]bar subtest[]]*")
def test_skip(pytester: pytest.Pytester, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setenv("COLUMNS", "120")
pytester.makepyfile(
"""
import pytest
def test_foo(subtests):
with subtests.test("foo subtest"):
pytest.skip("skip foo subtest")
def test_bar(subtests):
with subtests.test("bar subtest"):
pytest.skip("skip bar subtest")
pytest.skip("skip test_bar")
"""
)
result = pytester.runpytest("-ra")
result.stdout.fnmatch_lines(
[
"test_*.py .s * [[]100%[]]",
"*=== short test summary info ===*",
"SKIPPED [[]1[]] test_skip.py:9: skip test_bar",
"* 1 passed, 1 skipped in *",
]
)
result = pytester.runpytest("-v", "-ra")
result.stdout.fnmatch_lines(
[
"*.py::test_foo SUBSKIPPED[[]foo subtest[]] (skip foo subtest) * [[] 50%[]]",
"*.py::test_foo PASSED * [[] 50%[]]",
"*.py::test_bar SUBSKIPPED[[]bar subtest[]] (skip bar subtest) * [[]100%[]]",
"*.py::test_bar SKIPPED (skip test_bar) * [[]100%[]]",
"*=== short test summary info ===*",
"SUBSKIPPED[[]foo subtest[]] [[]1[]] *.py:*: skip foo subtest",
"SUBSKIPPED[[]foo subtest[]] [[]1[]] *.py:*: skip bar subtest",
"SUBSKIPPED[[]foo subtest[]] [[]1[]] *.py:*: skip test_bar",
"* 1 passed, 3 skipped in *",
]
)
pytester.makeini(
"""
[pytest]
verbosity_subtests = 0
"""
)
result = pytester.runpytest("-v", "-ra")
result.stdout.fnmatch_lines(
[
"*.py::test_foo PASSED * [[] 50%[]]",
"*.py::test_bar SKIPPED (skip test_bar) * [[]100%[]]",
"*=== short test summary info ===*",
"* 1 passed, 1 skipped in *",
]
)
result.stdout.no_fnmatch_line("*.py::test_foo SUBPASSED[[]foo subtest[]]*")
result.stdout.no_fnmatch_line("*.py::test_bar SUBPASSED[[]bar subtest[]]*")
result.stdout.no_fnmatch_line(
"SUBSKIPPED[[]foo subtest[]] [[]1[]] *.py:*: skip foo subtest"
)
result.stdout.no_fnmatch_line(
"SUBSKIPPED[[]foo subtest[]] [[]1[]] *.py:*: skip test_bar"
)
def test_xfail(pytester: pytest.Pytester, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setenv("COLUMNS", "120")
pytester.makepyfile(
"""
import pytest
def test_foo(subtests):
with subtests.test("foo subtest"):
pytest.xfail("xfail foo subtest")
def test_bar(subtests):
with subtests.test("bar subtest"):
pytest.xfail("xfail bar subtest")
pytest.xfail("xfail test_bar")
"""
)
result = pytester.runpytest("-ra")
result.stdout.fnmatch_lines(
[
"test_*.py .x * [[]100%[]]",
"*=== short test summary info ===*",
"* 1 passed, 1 xfailed in *",
]
)
result = pytester.runpytest("-v", "-ra")
result.stdout.fnmatch_lines(
[
"*.py::test_foo SUBXFAIL[[]foo subtest[]] (xfail foo subtest) * [[] 50%[]]",
"*.py::test_foo PASSED * [[] 50%[]]",
"*.py::test_bar SUBXFAIL[[]bar subtest[]] (xfail bar subtest) * [[]100%[]]",
"*.py::test_bar XFAIL (xfail test_bar) * [[]100%[]]",
"*=== short test summary info ===*",
"SUBXFAIL[[]foo subtest[]] *.py::test_foo - xfail foo subtest",
"SUBXFAIL[[]bar subtest[]] *.py::test_bar - xfail bar subtest",
"XFAIL *.py::test_bar - xfail test_bar",
"* 1 passed, 3 xfailed in *",
]
)
pytester.makeini(
"""
[pytest]
verbosity_subtests = 0
"""
)
result = pytester.runpytest("-v", "-ra")
result.stdout.fnmatch_lines(
[
"*.py::test_foo PASSED * [[] 50%[]]",
"*.py::test_bar XFAIL (xfail test_bar) * [[]100%[]]",
"*=== short test summary info ===*",
"* 1 passed, 1 xfailed in *",
]
)
result.stdout.no_fnmatch_line(
"SUBXFAIL[[]foo subtest[]] *.py::test_foo - xfail foo subtest"
)
result.stdout.no_fnmatch_line(
"SUBXFAIL[[]bar subtest[]] *.py::test_bar - xfail bar subtest"
)
def test_typing_exported(pytester: pytest.Pytester) -> None:
pytester.makepyfile(
"""
from pytest import Subtests
def test_typing_exported(subtests: Subtests) -> None:
assert isinstance(subtests, Subtests)
"""
)
result = pytester.runpytest()
result.stdout.fnmatch_lines(["*1 passed*"])
def test_subtests_and_parametrization(
pytester: pytest.Pytester, monkeypatch: pytest.MonkeyPatch
) -> None:
monkeypatch.setenv("COLUMNS", "120")
pytester.makepyfile(
"""
import pytest
@pytest.mark.parametrize("x", [0, 1])
def test_foo(subtests, x):
for i in range(3):
with subtests.test("custom", i=i):
assert i % 2 == 0
assert x == 0
"""
)
result = pytester.runpytest("-v")
result.stdout.fnmatch_lines(
[
"*.py::test_foo[[]0[]] SUBFAILED[[]custom[]] (i=1) *[[] 50%[]]",
"*.py::test_foo[[]0[]] FAILED *[[] 50%[]]",
"*.py::test_foo[[]1[]] SUBFAILED[[]custom[]] (i=1) *[[]100%[]]",
"*.py::test_foo[[]1[]] FAILED *[[]100%[]]",
"contains 1 failed subtest",
"* 4 failed, 4 subtests passed in *",
]
)
pytester.makeini(
"""
[pytest]
verbosity_subtests = 0
"""
)
result = pytester.runpytest("-v")
result.stdout.fnmatch_lines(
[
"*.py::test_foo[[]0[]] SUBFAILED[[]custom[]] (i=1) *[[] 50%[]]",
"*.py::test_foo[[]0[]] FAILED *[[] 50%[]]",
"*.py::test_foo[[]1[]] SUBFAILED[[]custom[]] (i=1) *[[]100%[]]",
"*.py::test_foo[[]1[]] FAILED *[[]100%[]]",
"contains 1 failed subtest",
"* 4 failed in *",
]
)
def test_subtests_fail_top_level_test(pytester: pytest.Pytester) -> None:
pytester.makepyfile(
"""
import pytest
def test_foo(subtests):
for i in range(3):
with subtests.test("custom", i=i):
assert i % 2 == 0
"""
)
result = pytester.runpytest("-v")
result.stdout.fnmatch_lines(
[
"* 2 failed, 2 subtests passed in *",
]
)
def test_subtests_do_not_overwrite_top_level_failure(pytester: pytest.Pytester) -> None:
pytester.makepyfile(
"""
import pytest
def test_foo(subtests):
for i in range(3):
with subtests.test("custom", i=i):
assert i % 2 == 0
assert False, "top-level failure"
"""
)
result = pytester.runpytest("-v")
result.stdout.fnmatch_lines(
[
"*AssertionError: top-level failure",
"* 2 failed, 2 subtests passed in *",
]
)
@pytest.mark.parametrize("flag", ["--last-failed", "--stepwise"])
def test_subtests_last_failed_step_wise(pytester: pytest.Pytester, flag: str) -> None:
"""Check that --last-failed and --step-wise correctly rerun tests with failed subtests."""
pytester.makepyfile(
"""
import pytest
def test_foo(subtests):
for i in range(3):
with subtests.test("custom", i=i):
assert i % 2 == 0
"""
)
result = pytester.runpytest("-v")
result.stdout.fnmatch_lines(
[
"* 2 failed, 2 subtests passed in *",
]
)
result = pytester.runpytest("-v", flag)
result.stdout.fnmatch_lines(
[
"* 2 failed, 2 subtests passed in *",
]
)
class TestUnittestSubTest:
"""Test unittest.TestCase.subTest functionality."""
def test_failures(
self, pytester: pytest.Pytester, monkeypatch: pytest.MonkeyPatch
) -> None:
monkeypatch.setenv("COLUMNS", "120")
pytester.makepyfile(
"""
from unittest import TestCase
class T(TestCase):
def test_foo(self):
with self.subTest("foo subtest"):
assert False, "foo subtest failure"
def test_bar(self):
with self.subTest("bar subtest"):
assert False, "bar subtest failure"
assert False, "test_bar also failed"
def test_zaz(self):
with self.subTest("zaz subtest"):
pass
"""
)
result = pytester.runpytest()
result.stdout.fnmatch_lines(
[
"* 3 failed, 2 passed in *",
]
)
def test_passes(
self, pytester: pytest.Pytester, monkeypatch: pytest.MonkeyPatch
) -> None:
monkeypatch.setenv("COLUMNS", "120")
pytester.makepyfile(
"""
from unittest import TestCase
class T(TestCase):
def test_foo(self):
with self.subTest("foo subtest"):
pass
def test_bar(self):
with self.subTest("bar subtest"):
pass
def test_zaz(self):
with self.subTest("zaz subtest"):
pass
"""
)
result = pytester.runpytest()
result.stdout.fnmatch_lines(
[
"* 3 passed in *",
]
)
def test_skip(
self,
pytester: pytest.Pytester,
) -> None:
pytester.makepyfile(
"""
from unittest import TestCase, main
class T(TestCase):
def test_foo(self):
for i in range(5):
with self.subTest(msg="custom", i=i):
if i % 2 == 0:
self.skipTest('even number')
"""
)
# This output might change #13756.
result = pytester.runpytest()
result.stdout.fnmatch_lines(["* 1 passed in *"])
def test_non_subtest_skip(
self, pytester: pytest.Pytester, monkeypatch: pytest.MonkeyPatch
) -> None:
monkeypatch.setenv("COLUMNS", "120")
pytester.makepyfile(
"""
from unittest import TestCase, main
class T(TestCase):
def test_foo(self):
with self.subTest(msg="subtest"):
assert False, "failed subtest"
self.skipTest('non-subtest skip')
"""
)
# This output might change #13756.
result = pytester.runpytest()
result.stdout.fnmatch_lines(
[
"SUBFAILED[[]subtest[]] test_non_subtest_skip.py::T::test_foo*",
"* 1 failed, 1 skipped in *",
]
)
def test_xfail(
self,
pytester: pytest.Pytester,
) -> None:
pytester.makepyfile(
"""
import pytest
from unittest import expectedFailure, TestCase
class T(TestCase):
@expectedFailure
def test_foo(self):
for i in range(5):
with self.subTest(msg="custom", i=i):
if i % 2 == 0:
raise pytest.xfail('even number')
if __name__ == '__main__':
main()
"""
)
# This output might change #13756.
result = pytester.runpytest()
result.stdout.fnmatch_lines(["* 1 xfailed in *"])
def test_only_original_skip_is_called(
self,
pytester: pytest.Pytester,
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""Regression test for pytest-dev/pytest-subtests#173."""
monkeypatch.setenv("COLUMNS", "120")
pytester.makepyfile(
"""
import unittest
from unittest import TestCase
@unittest.skip("skip this test")
class T(unittest.TestCase):
def test_foo(self):
assert 1 == 2
"""
)
result = pytester.runpytest("-v", "-rsf")
result.stdout.fnmatch_lines(
["SKIPPED [1] test_only_original_skip_is_called.py:6: skip this test"]
)
def test_skip_with_failure(
self,
pytester: pytest.Pytester,
monkeypatch: pytest.MonkeyPatch,
) -> None:
monkeypatch.setenv("COLUMNS", "120")
pytester.makepyfile(
"""
import pytest
from unittest import TestCase
class T(TestCase):
def test_foo(self):
with self.subTest("subtest 1"):
self.skipTest(f"skip subtest 1")
with self.subTest("subtest 2"):
assert False, "fail subtest 2"
"""
)
result = pytester.runpytest("-ra")
result.stdout.fnmatch_lines(
[
"*.py u. * [[]100%[]]",
"*=== short test summary info ===*",
"SUBFAILED[[]subtest 2[]] *.py::T::test_foo - AssertionError: fail subtest 2",
"* 1 failed, 1 passed in *",
]
)
result = pytester.runpytest("-v", "-ra")
result.stdout.fnmatch_lines(
[
"*.py::T::test_foo SUBSKIPPED[[]subtest 1[]] (skip subtest 1) * [[]100%[]]",
"*.py::T::test_foo SUBFAILED[[]subtest 2[]] * [[]100%[]]",
"*.py::T::test_foo PASSED * [[]100%[]]",
"SUBSKIPPED[[]subtest 1[]] [[]1[]] *.py:*: skip subtest 1",
"SUBFAILED[[]subtest 2[]] *.py::T::test_foo - AssertionError: fail subtest 2",
"* 1 failed, 1 passed, 1 skipped in *",
]
)
pytester.makeini(
"""
[pytest]
verbosity_subtests = 0
"""
)
result = pytester.runpytest("-v", "-ra")
result.stdout.fnmatch_lines(
[
"*.py::T::test_foo SUBFAILED[[]subtest 2[]] * [[]100%[]]",
"*.py::T::test_foo PASSED * [[]100%[]]",
"*=== short test summary info ===*",
r"SUBFAILED[[]subtest 2[]] *.py::T::test_foo - AssertionError: fail subtest 2",
r"* 1 failed, 1 passed in *",
]
)
result.stdout.no_fnmatch_line(
"*.py::T::test_foo SUBSKIPPED[[]subtest 1[]] (skip subtest 1) * [[]100%[]]"
)
result.stdout.no_fnmatch_line(
"SUBSKIPPED[[]subtest 1[]] [[]1[]] *.py:*: skip subtest 1"
)
class TestCapture:
def create_file(self, pytester: pytest.Pytester) -> None:
pytester.makepyfile(
"""
import sys
def test(subtests):
print()
print('start test')
with subtests.test(i='A'):
print("hello stdout A")
print("hello stderr A", file=sys.stderr)
assert 0
with subtests.test(i='B'):
print("hello stdout B")
print("hello stderr B", file=sys.stderr)
assert 0
print('end test')
assert 0
"""
)
@pytest.mark.parametrize("mode", ["fd", "sys"])
def test_capturing(self, pytester: pytest.Pytester, mode: str) -> None:
self.create_file(pytester)
result = pytester.runpytest(f"--capture={mode}")
result.stdout.fnmatch_lines(
[
"*__ test (i='A') __*",
"*Captured stdout call*",
"hello stdout A",
"*Captured stderr call*",
"hello stderr A",
"*__ test (i='B') __*",
"*Captured stdout call*",
"hello stdout B",
"*Captured stderr call*",
"hello stderr B",
"*__ test __*",
"*Captured stdout call*",
"start test",
"end test",
]
)
def test_no_capture(self, pytester: pytest.Pytester) -> None:
self.create_file(pytester)
result = pytester.runpytest("-s")
result.stdout.fnmatch_lines(
[
"start test",
"hello stdout A",
"uhello stdout B",
"uend test",
"*__ test (i='A') __*",
"*__ test (i='B') __*",
"*__ test __*",
]
)
result.stderr.fnmatch_lines(["hello stderr A", "hello stderr B"])
@pytest.mark.parametrize("fixture", ["capsys", "capfd"])
def test_capture_with_fixture(
self, pytester: pytest.Pytester, fixture: Literal["capsys", "capfd"]
) -> None:
pytester.makepyfile(
rf"""
import sys
def test(subtests, {fixture}):
print('start test')
with subtests.test(i='A'):
print("hello stdout A")
print("hello stderr A", file=sys.stderr)
out, err = {fixture}.readouterr()
assert out == 'start test\nhello stdout A\n'
assert err == 'hello stderr A\n'
"""
)
result = pytester.runpytest()
result.stdout.fnmatch_lines(
[
"*1 passed*",
]
)
class TestLogging:
def create_file(self, pytester: pytest.Pytester) -> None:
pytester.makepyfile(
"""
import logging
def test_foo(subtests):
logging.info("before")
with subtests.test("sub1"):
print("sub1 stdout")
logging.info("sub1 logging")
logging.debug("sub1 logging debug")
with subtests.test("sub2"):
print("sub2 stdout")
logging.info("sub2 logging")
logging.debug("sub2 logging debug")
assert False
"""
)
def test_capturing_info(self, pytester: pytest.Pytester) -> None:
self.create_file(pytester)
result = pytester.runpytest("--log-level=INFO")
result.stdout.fnmatch_lines(
[
"*___ test_foo [[]sub2[]] __*",
"*-- Captured stdout call --*",
"sub2 stdout",
"*-- Captured log call ---*",
"INFO * before",
"INFO * sub1 logging",
"INFO * sub2 logging",
"*== short test summary info ==*",
]
)
result.stdout.no_fnmatch_line("sub1 logging debug")
result.stdout.no_fnmatch_line("sub2 logging debug")
def test_capturing_debug(self, pytester: pytest.Pytester) -> None:
self.create_file(pytester)
result = pytester.runpytest("--log-level=DEBUG")
result.stdout.fnmatch_lines(
[
"*___ test_foo [[]sub2[]] __*",
"*-- Captured stdout call --*",
"sub2 stdout",
"*-- Captured log call ---*",
"INFO * before",
"INFO * sub1 logging",
"DEBUG * sub1 logging debug",
"INFO * sub2 logging",
"DEBUG * sub2 logging debug",
"*== short test summary info ==*",
]
)
def test_caplog(self, pytester: pytest.Pytester) -> None:
pytester.makepyfile(
"""
import logging
def test(subtests, caplog):
caplog.set_level(logging.INFO)
logging.info("start test")
with subtests.test("sub1"):
logging.info("inside %s", "subtest1")
assert len(caplog.records) == 2
assert caplog.records[0].getMessage() == "start test"
assert caplog.records[1].getMessage() == "inside subtest1"
"""
)
result = pytester.runpytest()
result.stdout.fnmatch_lines(
[
"*1 passed*",
]
)
def test_no_logging(self, pytester: pytest.Pytester) -> None:
pytester.makepyfile(
"""
import logging
def test(subtests):
logging.info("start log line")
with subtests.test("sub passing"):
logging.info("inside %s", "passing log line")
with subtests.test("sub failing"):
logging.info("inside %s", "failing log line")
assert False
logging.info("end log line")
"""
)
result = pytester.runpytest("-p no:logging")
result.stdout.fnmatch_lines(
[
"*2 failed in*",
]
)
result.stdout.no_fnmatch_line("*root:test_no_logging.py*log line*")
class TestDebugging:
"""Check --pdb support for subtests fixture and TestCase.subTest."""
class _FakePdb:
"""Fake debugger class implementation that tracks which methods were called on it."""
quitting: bool = False
calls: list[str] = []
def __init__(self, *_: object, **__: object) -> None:
self.calls.append("init")
def reset(self) -> None:
self.calls.append("reset")
def interaction(self, *_: object) -> None:
self.calls.append("interaction")
@pytest.fixture(autouse=True)
def cleanup_calls(self) -> None:
self._FakePdb.calls.clear()
def test_pdb_fixture(
self, pytester: pytest.Pytester, monkeypatch: pytest.MonkeyPatch
) -> None:
pytester.makepyfile(
"""
def test(subtests):
with subtests.test():
assert 0
"""
)
self.runpytest_and_check_pdb(pytester, monkeypatch)
def test_pdb_unittest(
self, pytester: pytest.Pytester, monkeypatch: pytest.MonkeyPatch
) -> None:
pytester.makepyfile(
"""
from unittest import TestCase
class Test(TestCase):
def test(self):
with self.subTest():
assert 0
"""
)
self.runpytest_and_check_pdb(pytester, monkeypatch)
def runpytest_and_check_pdb(
self, pytester: pytest.Pytester, monkeypatch: pytest.MonkeyPatch
) -> None:
# Install the fake pdb implementation in _pytest.subtests so we can reference
# it in the command line (any module would do).
import _pytest.subtests
monkeypatch.setattr(
_pytest.subtests, "_CustomPdb", self._FakePdb, raising=False
)
result = pytester.runpytest("--pdb", "--pdbcls=_pytest.subtests:_CustomPdb")
# Ensure pytest entered in debugging mode when encountering the failing
# assert.
result.stdout.fnmatch_lines("*entering PDB*")
assert self._FakePdb.calls == ["init", "reset", "interaction"]
def test_exitfirst(pytester: pytest.Pytester) -> None:
"""Validate that when passing --exitfirst the test exits after the first failed subtest."""
pytester.makepyfile(
"""
def test_foo(subtests):
with subtests.test("sub1"):
assert False
with subtests.test("sub2"):
assert False
"""
)
result = pytester.runpytest("--exitfirst")
assert result.parseoutcomes()["failed"] == 2
result.stdout.fnmatch_lines(
[
"SUBFAILED*[[]sub1[]] *.py::test_foo - assert False*",
"FAILED *.py::test_foo - assert False",
"* stopping after 2 failures*",
],
consecutive=True,
)
result.stdout.no_fnmatch_line("*sub2*") # sub2 not executed.
def test_do_not_swallow_pytest_exit(pytester: pytest.Pytester) -> None:
pytester.makepyfile(
"""
import pytest
def test(subtests):
with subtests.test():
pytest.exit()
def test2(): pass
"""
)
result = pytester.runpytest_subprocess()
result.stdout.fnmatch_lines(
[
"* _pytest.outcomes.Exit *",
"* 1 failed in *",
]
)
def test_nested(pytester: pytest.Pytester) -> None:
"""
Currently we do nothing special with nested subtests.
This test only sediments how they work now, we might reconsider adding some kind of nesting support in the future.
"""
pytester.makepyfile(
"""
import pytest
def test(subtests):
with subtests.test("a"):
with subtests.test("b"):
assert False, "b failed"
assert False, "a failed"
"""
)
result = pytester.runpytest_subprocess()
result.stdout.fnmatch_lines(
[
"SUBFAILED[b] test_nested.py::test - AssertionError: b failed",
"SUBFAILED[a] test_nested.py::test - AssertionError: a failed",
"* 3 failed in *",
]
)
def test_serialization() -> None:
from _pytest.subtests import pytest_report_from_serializable
from _pytest.subtests import pytest_report_to_serializable
report = SubtestReport(
"test_foo::test_foo",
("test_foo.py", 12, ""),
keywords={},
outcome="passed",
when="call",
longrepr=None,
context=SubtestContext(msg="custom message", kwargs=dict(i=10)),
)
data = pytest_report_to_serializable(report)
assert data is not None
new_report = pytest_report_from_serializable(data)
assert new_report is not None
assert new_report.context == SubtestContext(msg="custom message", kwargs=dict(i=10))
|