def test_works_with_filterwarnings(pytester: Pytester) -> None:
    """Ensure our warnings capture does not mess with pre-installed filters (#2430)."""
    pytester.makepyfile(
        """
        import warnings

        class MyWarning(Warning):
            pass

        warnings.filterwarnings("error", category=MyWarning)

        class TestWarnings(object):
            def test_my_warning(self):
                try:
                    warnings.warn(MyWarning("warn!"))
                    assert False
                except MyWarning:
                    assert True
    """
    )
    result = pytester.runpytest()
    result.stdout.fnmatch_lines(["*== 1 passed in *"])
Ensure our warnings capture does not mess with pre-installed filters (#2430).
testing/test_warnings.py