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

Matt McClure matthewlmcclure at gmail.com
Sat Aug 3 16:27:30 CEST 2013


Michael Foord <fuzzyman <at> voidspace.org.uk> writes:
> On 2 Aug 2013, at 19:19, Antoine Pitrou <solipsis <at> pitrou.net> wrote:
> > The patch is basically ready for commit, except for a possible doc
> > addition, no?
>
> Looks to be the case, reading the patch it looks fine. I'm currently on
holiday until Monday.
> If anyone is motivated to do the docs too and commit that would be great.
Otherwise I'll
> get to it on my return.

It looks like the patch is based on what will become 3.4. Would backporting
it to 2.7 be feasible?  What's involved in doing so?

I took a crack at the docs.

# HG changeset patch
# User Matt McClure <matthewlmcclure at gmail.com>
# Date 1375538965 14400
# Node ID d748d70201929288c230862da4dbdba33d61ae9f
# Parent  bf43956356ffe93e75ffdd5a7a8164fc68cf14ae
[11798] Document TestSuite.{__iter__, run} changes

diff --git a/Doc/library/unittest.rst b/Doc/library/unittest.rst
--- a/Doc/library/unittest.rst
+++ b/Doc/library/unittest.rst
@@ -1470,15 +1470,24 @@

       Tests grouped by a :class:`TestSuite` are always accessed by
iteration.
       Subclasses can lazily provide tests by overriding :meth:`__iter__`.
Note
-      that this method maybe called several times on a single suite
-      (for example when counting tests or comparing for equality)
-      so the tests returned must be the same for repeated iterations.
+      that this method may be called several times on a single suite (for
+      example when counting tests or comparing for equality) so the tests
+      returned by repeated iterations before :meth:`TestSuite.run` must be
the
+      same for each call iteration. After :meth:`TestSuite.run`, callers
should
+      not rely on the tests returned by this method unless the caller uses
a
+      subclass that overrides :meth:`TestSuite._removeTestAtIndex` to
preserve
+      test references.

       .. versionchanged:: 3.2
          In earlier versions the :class:`TestSuite` accessed tests
directly rather
          than through iteration, so overriding :meth:`__iter__` wasn't
sufficient
          for providing tests.

+      .. versionchanged:: 3.4
+         In earlier versions the :class:`TestSuite` held references to each
+         :class:`TestCase` after :meth:`TestSuite.run`. Subclasses can
restore
+         that behavior by overriding :meth:`TestSuite._removeTestAtIndex`.
+
    In the typical usage of a :class:`TestSuite` object, the :meth:`run`
method
    is invoked by a :class:`TestRunner` rather than by the end-user test
harness.

diff --git a/Lib/unittest/suite.py b/Lib/unittest/suite.py
--- a/Lib/unittest/suite.py
+++ b/Lib/unittest/suite.py
@@ -65,6 +65,7 @@
         return result

     def _removeTestAtIndex(self, index):
+        """Stop holding a reference to the TestCase at index."""
         try:
             self._tests[index] = None
         except TypeError:

-- 
Matt McClure
http://matthewlmcclure.com
http://www.mapmyfitness.com/profile/matthewlmcclure
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20130803/23d10a4a/attachment.html>


More information about the Python-Dev mailing list