@@ -545,6 +545,38 @@ def test_it():
545545 result .assert_outcomes ()
546546
547547
548+ def test_re_emit_uses_correct_module (pytester : Pytester ) -> None :
549+ warning_module_code = """
550+ import warnings
551+
552+ def trigger_warning(msg):
553+ warnings.warn(msg, UserWarning)
554+ """
555+ pytester .makepyfile (module_a = warning_module_code )
556+ pytester .makepyfile (module_b = warning_module_code )
557+
558+ test_code = """
559+ import pytest
560+ import warnings
561+ from module_a import trigger_warning as trigger_warning_a
562+ from module_b import trigger_warning as trigger_warning_b
563+
564+ def test_ignore_warning_from_module_a():
565+ with pytest.raises(pytest.fail.Exception, match="DID NOT WARN"):
566+ with pytest.warns(UserWarning, match="module A.") as outer:
567+ warnings.filterwarnings("ignore", category=UserWarning, module="module_a")
568+ with pytest.warns(UserWarning, match="module B.") as inner: # re-emit the module A warning
569+ trigger_warning_a("module A.")
570+ trigger_warning_b("module B.")
571+ """
572+ # Write the test to a new file called 'test_re_emit.py'
573+ pytester .makepyfile (test_re_emit = test_code )
574+
575+ # Run the test and assert that it passed
576+ result = pytester .runpytest ()
577+ result .assert_outcomes (passed = 1 )
578+
579+
548580def test_raise_type_error_on_invalid_warning () -> None :
549581 """Check pytest.warns validates warning messages are strings (#10865) or
550582 Warning instances (#11959)."""
0 commit comments