[TIP] Some simple examples for testing stuff
Michael Foord
fuzzyman at voidspace.org.uk
Wed Mar 10 05:48:28 PST 2010
On 09/03/2010 06:12, C. Titus Brown wrote:
> Hi all,
>
> I sat down and went through the various e-mails from the long testing/packaging
> thread, and implemented some of the mechanisms described therein; I just need
> code to get a feeling for things, ya know? ;)
>
> Anyway, I'd be interested in comments, corrections, and thoughts on the
> following.
>
> --
>
> Everything is under
>
> http://github.com/ctb/SomePackage/tree/master/examples/
>
> --
>
> 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.
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*.
Personally I would rather codify as a "best practise" that tests
shouldn't go in __init__.py but should all be in appropriate modules.
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!
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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20100310/fccc8601/attachment.htm>
More information about the testing-in-python
mailing list