[TIP] Some simple examples for testing stuff

Michael Foord fuzzyman at voidspace.org.uk
Wed Mar 10 09:54:55 PST 2010


On 10/03/2010 13:48, Michael Foord wrote:
> [snip...]
> Additionally, if you define load_tests in a package file then it gets 
> passed the 'pattern' argument (the third argument that  is normally 
> None when you define load_tests in test modules). It is then 
> responsible for continuing test discovery into the package itself. As 
> it gets passed the test loader this is easy. Here is a version of 
> load_tests that continues discovery:
>
> def load_tests(loader, tests, pattern):
>     if pattern is not None:
>         this_dir = os.path.abspath(os.path.dirname(__file__))
>         # continue discovery from this directory with the same
>         # pattern
>         suite = loader.discover(this_dir, pattern)
>         # this line adds tests from this __init__.py to the
>         # the suite created from discovery
>         suite.addTests(tests)
>         return suite
>     # just return default tests if we weren't loaded by test discovery
>     return tests
>
> Note - I need to check this works properly (it should!) but am on my 
> way out. Caveat emptor!

This recipe works fine - but you need to invoke test discovery with a 
pattern that will match the package name rather than only look in python 
modules. If you call your test package 'test_something' then the 
following will invoke the package load_tests function:

     unit2 discover -p 'test_*'

Michael
>
> If you ignore the comments it's only 7 lines of code. If it works 
> properly I should make sure this recipe is in the docs.
>
> All the best,
>
> Michael
>
>
>> --
>>
>> Using setuptools' 'test_suite' argument:
>>
>>   http://github.com/ctb/SomePackage/tree/master/examples/setuptools-test_suite/
>>
>> --
>>
>> Using setuptools' 'test_loader' argument:
>>
>>   http://github.com/ctb/SomePackage/tree/master/examples/setuptools-test_loader/
>>
>> --
>>
>> Building a fake TestSuite-like object:
>>
>>   http://github.com/ctb/SomePackage/tree/master/examples/fake_suite/
>>
>> --
>>
>> I'm particularly intrigued by the idea of coming up with a standard that
>> specifies some API-level compatibility with the discover protocol as well as
>> unittest behavior, e.g.
>>
>>     python -m unittest<something>
>>
>> but I didn't get a chance to figure this out tonight.  Code welcome,
>> esp in the context of the last (fake_suite) example.
>>
>> cheers,
>> --titus
>>    
>
>
> -- 
> http://www.ironpythoninaction.com/
> http://www.voidspace.org.uk/blog
>
> 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.
>
>    
>
>
> _______________________________________________
> testing-in-python mailing list
> testing-in-python at lists.idyll.org
> http://lists.idyll.org/listinfo/testing-in-python
>    


-- 
http://www.ironpythoninaction.com/
http://www.voidspace.org.uk/blog

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.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20100310/4fae691c/attachment-0001.htm>


More information about the testing-in-python mailing list