def test_teardown_issue1649(pytester: Pytester) -> None:
    """
    Are TestCase objects cleaned up? Often unittest TestCase objects set
    attributes that are large and expensive during test run or setUp.

    The TestCase will not be cleaned up if the test fails, because it
    would then exist in the stackframe.

    Regression test for #1649 (see also #12367).
    """
    pytester.makepyfile(
        """
        import unittest
        import gc

        class TestCaseObjectsShouldBeCleanedUp(unittest.TestCase):
            def test_expensive(self):
                self.an_expensive_obj = object()

            def test_is_it_still_alive(self):
                gc.collect()
                for obj in gc.get_objects():
                    if type(obj).__name__ == "TestCaseObjectsShouldBeCleanedUp":
                        assert not hasattr(obj, "an_expensive_obj")
                        break
                else:
                    assert False, "Could not find TestCaseObjectsShouldBeCleanedUp instance"
        """
    )

    result = pytester.runpytest()
    assert result.ret == ExitCode.OK

    Are TestCase objects cleaned up? Often unittest TestCase objects set
    attributes that are large and expensive during test run or setUp.

    The TestCase will not be cleaned up if the test fails, because it
    would then exist in the stackframe.

    Regression test for #1649 (see also #12367).
    
testing/test_unittest.py