Ensure that fixture objects are properly destroyed by the garbage collector at the end of their expected life-times (#2981).
(pytester: Pytester)
| 1033 | |
| 1034 | |
| 1035 | def test_fixture_values_leak(pytester: Pytester) -> None: |
| 1036 | """Ensure that fixture objects are properly destroyed by the garbage collector at the end of their expected |
| 1037 | life-times (#2981). |
| 1038 | """ |
| 1039 | pytester.makepyfile( |
| 1040 | """ |
| 1041 | import attr |
| 1042 | import gc |
| 1043 | import pytest |
| 1044 | import weakref |
| 1045 | |
| 1046 | @attr.s |
| 1047 | class SomeObj(object): |
| 1048 | name = attr.ib() |
| 1049 | |
| 1050 | fix_of_test1_ref = None |
| 1051 | session_ref = None |
| 1052 | |
| 1053 | @pytest.fixture(scope='session') |
| 1054 | def session_fix(): |
| 1055 | global session_ref |
| 1056 | obj = SomeObj(name='session-fixture') |
| 1057 | session_ref = weakref.ref(obj) |
| 1058 | return obj |
| 1059 | |
| 1060 | @pytest.fixture |
| 1061 | def fix(session_fix): |
| 1062 | global fix_of_test1_ref |
| 1063 | obj = SomeObj(name='local-fixture') |
| 1064 | fix_of_test1_ref = weakref.ref(obj) |
| 1065 | return obj |
| 1066 | |
| 1067 | def test1(fix): |
| 1068 | assert fix_of_test1_ref() is fix |
| 1069 | |
| 1070 | def test2(): |
| 1071 | gc.collect() |
| 1072 | # fixture "fix" created during test1 must have been destroyed by now |
| 1073 | assert fix_of_test1_ref() is None |
| 1074 | """ |
| 1075 | ) |
| 1076 | # Running on subprocess does not activate the HookRecorder |
| 1077 | # which holds itself a reference to objects in case of the |
| 1078 | # pytest_assert_reprcompare hook |
| 1079 | result = pytester.runpytest_subprocess() |
| 1080 | result.stdout.fnmatch_lines(["* 2 passed *"]) |
| 1081 | |
| 1082 | |
| 1083 | def test_fixture_order_respects_scope(pytester: Pytester) -> None: |
nothing calls this directly
no test coverage detected