|
| 1 | +import os |
1 | 2 | import warnings |
2 | 3 |
|
3 | 4 | import pytest |
@@ -640,3 +641,96 @@ def pytest_configure(): |
640 | 641 | assert "INTERNALERROR" not in result.stderr.str() |
641 | 642 | warning = recwarn.pop() |
642 | 643 | assert str(warning.message) == "from pytest_configure" |
| 644 | + |
| 645 | + |
| 646 | +class TestStackLevel: |
| 647 | + def test_issue4445_rewrite(self, testdir): |
| 648 | + """#4445: Make sure the warning points to a reasonable location |
| 649 | + See origin of _issue_warning_captured at: _pytest.assertion.rewrite.py:241 |
| 650 | + """ |
| 651 | + testdir.makepyfile(some_mod="") |
| 652 | + testdir.makeconftest( |
| 653 | + """ |
| 654 | + import some_mod |
| 655 | + import pytest |
| 656 | +
|
| 657 | + pytest.register_assert_rewrite("some_mod") |
| 658 | + """ |
| 659 | + ) |
| 660 | + result = testdir.runpytest() |
| 661 | + # with stacklevel=5 the warning originates from register_assert_rewrite |
| 662 | + # function in the created conftest.py |
| 663 | + result.stdout.fnmatch_lines( |
| 664 | + ["*conftest.py:4*", "*Module already imported*", "*some_mod*"] |
| 665 | + ) |
| 666 | + |
| 667 | + def test_issue4445_preparse(self, testdir): |
| 668 | + """#4445: Make sure the warning points to a reasonable location |
| 669 | + See origin of _issue_warning_captured at: _pytest.config.__init__.py:910 |
| 670 | + """ |
| 671 | + testdir.makeconftest( |
| 672 | + """ |
| 673 | + import nothing |
| 674 | + """ |
| 675 | + ) |
| 676 | + result = testdir.runpytest("--help") |
| 677 | + # with stacklevel=2 the warning originates from config._preparse and is |
| 678 | + # thrown by an errorneous conftest.py |
| 679 | + result.stdout.fnmatch_lines_random( |
| 680 | + [ |
| 681 | + "*config{sep}__init__.py:915*".format(sep=os.sep), |
| 682 | + "*could not load initial conftests*", |
| 683 | + ] |
| 684 | + ) |
| 685 | + |
| 686 | + def test_issue4445_import_plugin(self, testdir): |
| 687 | + """#4445: Make sure the warning points to a reasonable location |
| 688 | + See origin of _issue_warning_captured at: _pytest.config.__init__.py:585 |
| 689 | + """ |
| 690 | + testdir.makepyfile( |
| 691 | + some_plugin=""" |
| 692 | + import pytest |
| 693 | + pytest.skip("thing", allow_module_level=True) |
| 694 | + """ |
| 695 | + ) |
| 696 | + result = testdir.runpytest("-p", "some_plugin", syspathinsert=True) |
| 697 | + # with stacklevel=2 the warning originates from |
| 698 | + # config.PytestPluginManager.import_plugin is thrown by a skipped plugin |
| 699 | + result.stdout.fnmatch_lines( |
| 700 | + [ |
| 701 | + "*config{sep}__init__.py:588*".format(sep=os.sep), |
| 702 | + "*skipped plugin 'some_plugin': thing*", |
| 703 | + ] |
| 704 | + ) |
| 705 | + |
| 706 | + def test_issue4445_resultlog(self, testdir): |
| 707 | + """#4445: Make sure the warning points to a reasonable location |
| 708 | + See origin of _issue_warning_captured at: _pytest.resultlog.py:35 |
| 709 | + """ |
| 710 | + testdir.makepyfile( |
| 711 | + """ |
| 712 | + def test_dummy(): |
| 713 | + pass |
| 714 | + """ |
| 715 | + ) |
| 716 | + result = testdir.runpytest( |
| 717 | + "--result-log={dir}".format(dir=testdir.tmpdir.join("result.log")) |
| 718 | + ) |
| 719 | + # with stacklevel=2 the warning originates from resultlog.pytest_configure |
| 720 | + # and is thrown when --result-log is used |
| 721 | + result.stdout.fnmatch_lines( |
| 722 | + ["*resultlog.py:35*", "*--result-log is deprecated*"] |
| 723 | + ) |
| 724 | + |
| 725 | + def test_issue4445_cacheprovider_set(self, testdir): |
| 726 | + """#4445: Make sure the warning points to a reasonable location |
| 727 | + See origin of _issue_warning_captured at: _pytest.cacheprovider.py:59 |
| 728 | + """ |
| 729 | + testdir.tmpdir.join(".pytest_cache").write("something wrong") |
| 730 | + result = testdir.runpytest() |
| 731 | + # with stacklevel=3 the warning originates from one stacklevel above |
| 732 | + # _issue_warning_captured in cacheprovider.Cache.set and is thrown |
| 733 | + # when there are errors during cache folder creation |
| 734 | + result.stdout.fnmatch_lines( |
| 735 | + ["*cacheprovider.py:120*", "*could not create cache path*"] |
| 736 | + ) |
0 commit comments