|
1 | 1 | # mypy: allow-untyped-defs
|
2 |
| -import gc |
3 | 2 | import sys
|
4 | 3 | from typing import List
|
5 | 4 |
|
@@ -192,30 +191,35 @@ def test_check(self):
|
192 | 191 | def test_teardown_issue1649(pytester: Pytester) -> None:
|
193 | 192 | """
|
194 | 193 | Are TestCase objects cleaned up? Often unittest TestCase objects set
|
195 |
| - attributes that are large and expensive during setUp. |
| 194 | + attributes that are large and expensive during test run or setUp. |
196 | 195 |
|
197 | 196 | The TestCase will not be cleaned up if the test fails, because it
|
198 | 197 | would then exist in the stackframe.
|
| 198 | +
|
| 199 | + Regression test for #1649 (see also #12367). |
199 | 200 | """
|
200 |
| - testpath = pytester.makepyfile( |
| 201 | + pytester.makepyfile( |
201 | 202 | """
|
202 | 203 | import unittest
|
203 |
| - class TestCaseObjectsShouldBeCleanedUp(unittest.TestCase): |
204 |
| - def setUp(self): |
205 |
| - self.an_expensive_object = 1 |
206 |
| - def test_demo(self): |
207 |
| - pass |
| 204 | + import gc |
208 | 205 |
|
209 |
| - """ |
| 206 | + class TestCaseObjectsShouldBeCleanedUp(unittest.TestCase): |
| 207 | + def test_expensive(self): |
| 208 | + self.an_expensive_obj = object() |
| 209 | +
|
| 210 | + def test_is_it_still_alive(self): |
| 211 | + gc.collect() |
| 212 | + for obj in gc.get_objects(): |
| 213 | + if type(obj).__name__ == "TestCaseObjectsShouldBeCleanedUp": |
| 214 | + assert not hasattr(obj, "an_expensive_obj") |
| 215 | + break |
| 216 | + else: |
| 217 | + assert False, "Could not find TestCaseObjectsShouldBeCleanedUp instance" |
| 218 | + """ |
210 | 219 | )
|
211 | 220 |
|
212 |
| - pytester.inline_run("-s", testpath) |
213 |
| - gc.collect() |
214 |
| - |
215 |
| - # Either already destroyed, or didn't run setUp. |
216 |
| - for obj in gc.get_objects(): |
217 |
| - if type(obj).__name__ == "TestCaseObjectsShouldBeCleanedUp": |
218 |
| - assert not hasattr(obj, "an_expensive_obj") |
| 221 | + result = pytester.runpytest() |
| 222 | + assert result.ret == ExitCode.OK |
219 | 223 |
|
220 | 224 |
|
221 | 225 | def test_unittest_skip_issue148(pytester: Pytester) -> None:
|
|
0 commit comments