[TIP] new discovery.py - q&a???

Michael Foord fuzzyman at voidspace.org.uk
Mon May 4 10:43:02 PDT 2009

Doug Philips wrote:
> On or about Monday, May 04, 2009, at 01:04PM, Michael Foord indited:
>> I assume you mean:
>>    In a *package* context, 'load_tests' is really a filtery kind of 
>> thing where as in a module context it is only a filter by a stretch of 
>> filtering nothingness into a sequence of tests. :)
> Ok, so I am even more confused: (line numbers taken from: http://twurl.nl/nz8f4y)
> Looking at _find_tests(), line 89 is testing for a file (module), and line 93 is yielding loadTestsFromModule.
> On line 25 (in loadTestsFromModule) is calling the super class to do the loading and then passing the loaded tests into the modules 'load_tests' function (if it exists), and that is the use of 'load_tests' as a filter that I was referring to.
> On line 115 is where the package's load_tests' function is used as a filter from nothing-ness to a test sequence. :)

Perhaps this will make it simpler. load_tests, if present, always 
returns a test suite and is used to customize the loading of tests for 
the module or package it is defined in.

    load_tests in a module customizes loading tests for just that 
module. It can be called when tests are loaded from the module normally 
or from test discovery.

    load_tests in a package is only used for the purposes of test 
discovery. It controls loading tests for the *whole* package. Discovery 
does not continue into the package, but as load_tests is passed the 
loader instance and the filter in use it is free to continue (and 
potentially modify) discovery into that package.

So they serve the same purpose but are called in different 
circumstances. A potential source of confusion is that in Python a 
package is just a module; so if you import the package as an object and 
pass it to loadTestsFromModule then load_tests will be called in the 
normal way for a module. This is why I think they need to have the same 

There is obviously no need to ever define load_tests (it is a 
customization protocol), and if you do how they will be called will 
depend on how *your* test framework uses them. You only need to support 
the scenarios you want to use...

>> loader.discover(...) returns a test suite as normal and can be used in 
>> any which way you please...
> understood.
> -Doug


More information about the testing-in-python mailing list