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