[TIP] Some simple examples for testing stuff

Michael Foord fuzzyman at voidspace.org.uk
Wed Mar 10 14:48:54 PST 2010

On 10/03/2010 21:50, C. Titus Brown wrote:
> On Wed, Mar 10, 2010 at 01:48:28PM +0000, Michael Foord wrote:
>> On 09/03/2010 06:12, C. Titus Brown wrote:
>>> An example of using 'discover':
>>>    http://github.com/ctb/SomePackage/tree/master/examples/discover/
>> Hi Titus,
>> As I said to you in a private email, but perhaps it bears discussing
>> here,  test discovery (unittest2 or the discover module) doesn't
>> automatically look in package files (__init__.py) by default.
>> That's actually the expected (by me) and documented behaviour - although
>> whether it is correct or not is debatable, and I'm open to discussion on
>> the subject.
> Hi Michael,
> I guess I was confused by this paragraph in the docs at http://pypi.python.org/pypi/discover/0.3.2:
> """
> If a test package name (directory with __init__.py) matches the pattern then
> the package will be checked for a load_tests function. If this exists then it
> will be called with loader, tests, pattern.
> """
> I interpreted that to mean "if a test package name matches the *test* pattern
> then..." which apparently it does not?

The critical part of that sentence is 'if'... By default the pattern 
includes '.py' so directories (package name) won't match - however you 
are free to change the pattern so that they do (and if a package name 

I'll try and make it clearer that by default package names won't match, 
but you are free to change the pattern so that they do match.

>> Part of the reason for it is my distaste for putting anything
>> (especially tests) in your __init__.py. It is also relatively common
>> practise to explicitly import your tests from sub-modules into the
>> package level __init__ - so that custom collection code only need import
>> them from the package and not have to visit all the submodules. Where
>> this is the case, visiting the package as well as the sub-modules in
>> discovery by default would find every test *twice*.
> Good point; I was just confused by the docs.
> cheers,
> --titus


READ CAREFULLY. By accepting and reading this email you agree, on behalf of your employer, to release me from all obligations and waivers arising from any and all NON-NEGOTIATED agreements, licenses, terms-of-service, shrinkwrap, clickwrap, browsewrap, confidentiality, non-disclosure, non-compete and acceptable use policies (”BOGUS AGREEMENTS”) that I have entered into with your employer, its partners, licensors, agents and assigns, in perpetuity, without prejudice to my ongoing rights and privileges. You further represent that you have the authority to release me from any BOGUS AGREEMENTS on behalf of your employer.

More information about the testing-in-python mailing list