[Python-Dev] unittest enhancement for TestCase classes hierarchies

Collin Winter collinw at gmail.com
Sat Mar 10 21:54:58 CET 2007


On 3/10/07, Jean-Paul Calderone <exarkun at divmod.com> wrote:
> On Sat, 10 Mar 2007 09:13:28 -0600, Collin Winter <collinw at gmail.com> wrote:
> >In my continuing trawl through the SF patch tracker, I came across
> >#1244929 (http://python.org/sf/1244929), which causes
> >TestLoader.loadTestsFromModule() to skip classes whose name starts
> >with an underscore. This addresses the warning in that method's docs:
> >
> >"""
> >While using a hierarchy of
> >TestCase-derived classes can be convenient in sharing
> >fixtures and helper functions, defining test methods on base classes
> >that are not intended to be instantiated directly does not play well
> >with this method.  Doing so, however, can be useful when the
> >fixtures are different and defined in subclasses.
> >"""
> >
> >"Does not play well", in this case, means that your base classes will
> >be picked up against your will if they subclass TestCase.
> >
> >I like the patch and have worked up tests and doc changes for it. Any
> >objections to including this in 2.6?
>
> This use case is what mixins are for.  You don't have to include TestCase
> in your ancestry until you get to a class which you actually want to run
> tests.
>
> The current rule of loading anything that subclasses TestCase is simple
> and straightforward.  Complicating it to provide a feature which is already
> available through a widely used standard Python idiom doesn't seem worth
> while.

Understood, but I don't like having to keep repeating, "yes, this
subclass of X is also a TestCase; so is this one, and this one, and
this one, etc". Python already ignores _-prefixed names in certain
analogous, load-everything-from-a-module situations ("from x import
*"), so I don't see this as being particularly unpythonic.

Nor do I see it as all that complicated. Striking the current
equivocating, paragraph-long warning from loadTestsFromModule()'s docs
and replacing it with "Classes whose name starts with an underscore
will be ignored" seems like a win to me.

Collin Winter


More information about the Python-Dev mailing list