|
5 | 5 | from typing import Type
|
6 | 6 | import warnings
|
7 | 7 |
|
8 |
| -from _pytest.pytester import Pytester |
9 |
| -from _pytest.recwarn import WarningsRecorder |
10 | 8 | import pytest
|
| 9 | +from pytest import ExitCode |
| 10 | +from pytest import Pytester |
| 11 | +from pytest import WarningsRecorder |
11 | 12 |
|
12 | 13 |
|
13 | 14 | def test_recwarn_stacklevel(recwarn: WarningsRecorder) -> None:
|
@@ -479,6 +480,71 @@ def test_catch_warning_within_raise(self) -> None:
|
479 | 480 | warnings.warn("some warning", category=FutureWarning)
|
480 | 481 | raise ValueError("some exception")
|
481 | 482 |
|
| 483 | + def test_skip_within_warns(self, pytester: Pytester) -> None: |
| 484 | + """Regression test for #11907.""" |
| 485 | + pytester.makepyfile( |
| 486 | + """ |
| 487 | + import pytest |
| 488 | +
|
| 489 | + def test_it(): |
| 490 | + with pytest.warns(Warning): |
| 491 | + pytest.skip("this is OK") |
| 492 | + """, |
| 493 | + ) |
| 494 | + |
| 495 | + result = pytester.runpytest() |
| 496 | + assert result.ret == ExitCode.OK |
| 497 | + result.assert_outcomes(skipped=1) |
| 498 | + |
| 499 | + def test_fail_within_warns(self, pytester: Pytester) -> None: |
| 500 | + """Regression test for #11907.""" |
| 501 | + pytester.makepyfile( |
| 502 | + """ |
| 503 | + import pytest |
| 504 | +
|
| 505 | + def test_it(): |
| 506 | + with pytest.warns(Warning): |
| 507 | + pytest.fail("BOOM") |
| 508 | + """, |
| 509 | + ) |
| 510 | + |
| 511 | + result = pytester.runpytest() |
| 512 | + assert result.ret == ExitCode.TESTS_FAILED |
| 513 | + result.assert_outcomes(failed=1) |
| 514 | + assert "DID NOT WARN" not in str(result.stdout) |
| 515 | + |
| 516 | + def test_exit_within_warns(self, pytester: Pytester) -> None: |
| 517 | + """Regression test for #11907.""" |
| 518 | + pytester.makepyfile( |
| 519 | + """ |
| 520 | + import pytest |
| 521 | +
|
| 522 | + def test_it(): |
| 523 | + with pytest.warns(Warning): |
| 524 | + pytest.exit() |
| 525 | + """, |
| 526 | + ) |
| 527 | + |
| 528 | + result = pytester.runpytest() |
| 529 | + assert result.ret == ExitCode.INTERRUPTED |
| 530 | + result.assert_outcomes() |
| 531 | + |
| 532 | + def test_keyboard_interrupt_within_warns(self, pytester: Pytester) -> None: |
| 533 | + """Regression test for #11907.""" |
| 534 | + pytester.makepyfile( |
| 535 | + """ |
| 536 | + import pytest |
| 537 | +
|
| 538 | + def test_it(): |
| 539 | + with pytest.warns(Warning): |
| 540 | + raise KeyboardInterrupt() |
| 541 | + """, |
| 542 | + ) |
| 543 | + |
| 544 | + result = pytester.runpytest_subprocess() |
| 545 | + assert result.ret == ExitCode.INTERRUPTED |
| 546 | + result.assert_outcomes() |
| 547 | + |
482 | 548 |
|
483 | 549 | def test_raise_type_error_on_non_string_warning() -> None:
|
484 | 550 | """Check pytest.warns validates warning messages are strings (#10865)."""
|
|
0 commit comments