[Python-Dev] unittest.TestSuite holding references to unittest.TestCase instances too long

Matt McClure matthewlmcclure at gmail.com
Fri Aug 2 13:51:23 CEST 2013

It seems unittest.TestSuite holds references to unittest.TestCase instances
after the test runs, until the test suite finishes. In a large suite, where
the TestCase instances consume memory during execution, that can lead to
exhausting all available memory and the OS killing the test process.

What do you think of a change like this?

$ hg diff
diff -r 3bd55ec317a7 Lib/unittest/suite.py
--- a/Lib/unittest/suite.py Thu Aug 01 23:57:21 2013 +0200
+++ b/Lib/unittest/suite.py Fri Aug 02 07:42:22 2013 -0400
@@ -90,7 +90,12 @@
         if getattr(result, '_testRunEntered', False) is False:
             result._testRunEntered = topLevel = True

-        for test in self:
+        while True:
+            try:
+                test = self._tests.pop(0)
+            except IndexError:
+                break
             if result.shouldStop:

See also the conversation on django-developers[1] that led me here.

[1]: https://groups.google.com/forum/#!topic/django-developers/XUMetDSGVT0

Matt McClure
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20130802/c2ee7292/attachment.html>

More information about the Python-Dev mailing list