Ensure functions added with addCleanup are always called after each test ends (#6947)
(pytester: Pytester)
| 918 | |
| 919 | |
| 920 | def 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 | |
| 951 | def test_issue333_result_clearing(pytester: Pytester) -> None: |
nothing calls this directly
no test coverage detected