Skip to content

Commit c446fde

Browse files
committed
(tests) Add tearDown to modelExporters to improve model-related memory handling in the testing pipeline
1 parent 38d1849 commit c446fde

1 file changed

Lines changed: 16 additions & 0 deletions

File tree

pysipfenn/tests/test_ModelExporters.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,22 @@ def setUp(self):
2121
self.c = pysipfenn.Calculator()
2222
self.assertIsNotNone(self.c)
2323

24+
def tearDown(self):
25+
'''Release all objects created during setUp and by individual tests to prevent memory
26+
from accumulating across the test suite. Exporter attributes (``onnxexp``,
27+
``torchexp``, ``coremlexp``) are deleted inside their respective tests on the happy
28+
path, but a mid-test failure would skip those clean-ups; the ``hasattr`` guards here
29+
ensure they are always freed. The Calculator is deleted last because the exporters
30+
hold a ``calculator`` reference into it.
31+
'''
32+
# Optional exporter objects — only present on the happy path when the test fails early
33+
for attr in ('onnxexp', 'torchexp', 'coremlexp'):
34+
if hasattr(self, attr):
35+
delattr(self, attr)
36+
# Core Calculator created in setUp
37+
del self.c
38+
gc.collect()
39+
2440
def testInit(self):
2541
'''Test that the Calculator object is initialised correctly.'''
2642
self.assertEqual(self.c.predictions, [])

0 commit comments

Comments
 (0)