[TIP] Test discovery for unittest

Robert Collins robertc at robertcollins.net
Mon Apr 6 15:31:42 PDT 2009

On Mon, 2009-04-06 at 23:17 +0100, Michael Foord wrote:
> I'm happy with a package / module level hook that takes loader and tests 
> arguments and is called load_tests or test_suite.
> It should return a test suite or None, and if available at the package 
> level the current implementation won't continue discovery into the 
> package (allowing a different return value at some future point to 
> modify discovery for the package would be possible).
> Now one of us needs to implement it. :-)

I will happily do a patch up. Easter is coming up :).

This has been largely a you-and-I discussion, does anyone else have
things they want that are not satisfied by this?

The following is the guts of the patch: everything else is tests and

 * Alter TestLoader.loadTestsFromModule:
        tests = []
        for name in dir(module):
            obj = getattr(module, name)
            if (isinstance(obj, (type, types.ClassType)) and
                issubclass(obj, TestCase)):
+        result = self.suiteClass(tests)
+        load_hook = getattr(module, 'load_tests', None)
+        if load_hook is not None:
+            result = load_hook(self, result)
+        if not result:
+            return self.suiteClass()
+        else:
+            return result
-        return self.suiteClass(tests)

(I've chosen load_tests because AFAIK its only used by bzr at the
moment, and this prevent causing trouble for trial and other general
purpose test environments that do use test_suite at the moment with a
different signature.)

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 197 bytes
Desc: This is a digitally signed message part
Url : http://lists.idyll.org/pipermail/testing-in-python/attachments/20090407/544cbf88/attachment-0001.pgp 

More information about the testing-in-python mailing list