def test_cleanup_functions(pytester: Pytester) -> None:
    """Ensure functions added with addCleanup are always called after each test ends (#6947)"""
    pytester.makepyfile(
        """
        import unittest

        cleanups = []

        class Test(unittest.TestCase):

            def test_func_1(self):
                self.addCleanup(cleanups.append, "test_func_1")

            def test_func_2(self):
                self.addCleanup(cleanups.append, "test_func_2")
                assert 0

            def test_func_3_check_cleanups(self):
                assert cleanups == ["test_func_1", "test_func_2"]
    """
    )
    result = pytester.runpytest("-v")
    result.stdout.fnmatch_lines(
        [
            "*::test_func_1 PASSED *",
            "*::test_func_2 FAILED *",
            "*::test_func_3_check_cleanups PASSED *",
        ]
    )
Ensure functions added with addCleanup are always called after each test ends (#6947)
testing/test_unittest.py