[TIP] Test discovery for unittest
fuzzyman at voidspace.org.uk
Mon Apr 6 20:01:02 PDT 2009
Robert Collins wrote:
> 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.)
But a technique based on this patch won't support the 'abort recursing
into packages if load_test' is found behavior - at least not as is.
I'm fine with having load_test called from inside the loader, as
presumably this matches your general use case even without test
discovery, but discovery has to know whether load_test was found (or
maybe it can tell just from a hasattr on the module?).
More information about the testing-in-python