MCPcopy Create free account
hub / github.com/pytest-dev/pytest / test_cleanup_functions

Function test_cleanup_functions

testing/test_unittest.py:920–948  ·  view source on GitHub ↗

Ensure functions added with addCleanup are always called after each test ends (#6947)

(pytester: Pytester)

Source from the content-addressed store, hash-verified

918
919
920def test_cleanup_functions(pytester: Pytester) -> None:
921 """Ensure functions added with addCleanup are always called after each test ends (#6947)"""
922 pytester.makepyfile(
923 """
924 import unittest
925
926 cleanups = []
927
928 class Test(unittest.TestCase):
929
930 def test_func_1(self):
931 self.addCleanup(cleanups.append, "test_func_1")
932
933 def test_func_2(self):
934 self.addCleanup(cleanups.append, "test_func_2")
935 assert 0
936
937 def test_func_3_check_cleanups(self):
938 assert cleanups == ["test_func_1", "test_func_2"]
939 """
940 )
941 result = pytester.runpytest("-v")
942 result.stdout.fnmatch_lines(
943 [
944 "*::test_func_1 PASSED *",
945 "*::test_func_2 FAILED *",
946 "*::test_func_3_check_cleanups PASSED *",
947 ]
948 )
949
950
951def test_issue333_result_clearing(pytester: Pytester) -> None:

Callers

nothing calls this directly

Calls 3

fnmatch_linesMethod · 0.80
makepyfileMethod · 0.45
runpytestMethod · 0.45

Tested by

no test coverage detected