<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta content="text/html; charset=ISO-8859-1"
 http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
On 09/03/2010 06:12, C. Titus Brown wrote:
<blockquote cite="mid:20100309061210.GA20444@idyll.org" type="cite">
  <pre wrap="">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 

 <a class="moz-txt-link-freetext" href="http://github.com/ctb/SomePackage/tree/master/examples/">http://github.com/ctb/SomePackage/tree/master/examples/</a>

--

An example of using 'discover':

 <a class="moz-txt-link-freetext" href="http://github.com/ctb/SomePackage/tree/master/examples/discover/">http://github.com/ctb/SomePackage/tree/master/examples/discover/</a>
  </pre>
</blockquote>
<br>
Hi Titus,<br>
<br>
As I said to you in a private email, but perhaps it bears discussing
here,&nbsp; test discovery (unittest2 or the discover module) doesn't
automatically look in package files (__init__.py) by default.<br>
<br>
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.
<br>
<br>
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 <b class="moz-txt-star"><span
 class="moz-txt-tag">*</span>twice<span class="moz-txt-tag">*</span></b>.
<br>
<br>
Personally I would rather codify as a "best practise" that tests
shouldn't go in __init__.py but should all be in appropriate modules.
<br>
<br>
Additionally, if you define load_tests in a package file then it gets
passed the 'pattern' argument (the third argument that&nbsp; 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:<br>
<br>
def load_tests(loader, tests, pattern):<br>
&nbsp;&nbsp;&nbsp; if pattern is not None:<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; this_dir = os.path.abspath(os.path.dirname(__file__))<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; # continue discovery from this directory with the same <br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; # pattern<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; suite = loader.discover(this_dir, pattern)<br>
&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; # this line adds tests from this __init__.py to the <br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; # the suite created from discovery<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; suite.addTests(tests)<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return suite<br>
&nbsp;&nbsp;&nbsp; # just return default tests if we weren't loaded by test discovery<br>
&nbsp;&nbsp;&nbsp; return tests<br>
<br>
Note - I need to check this works properly (it should!) but am on my
way out. Caveat emptor!<br>
<br>
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.<br>
<br>
All the best,
<br>
<br>
Michael
<br>
<br>
<br>
<blockquote cite="mid:20100309061210.GA20444@idyll.org" type="cite">
  <pre wrap="">
--

Using setuptools' 'test_suite' argument:

 <a class="moz-txt-link-freetext" href="http://github.com/ctb/SomePackage/tree/master/examples/setuptools-test_suite/">http://github.com/ctb/SomePackage/tree/master/examples/setuptools-test_suite/</a>

--

Using setuptools' 'test_loader' argument:

 <a class="moz-txt-link-freetext" href="http://github.com/ctb/SomePackage/tree/master/examples/setuptools-test_loader/">http://github.com/ctb/SomePackage/tree/master/examples/setuptools-test_loader/</a>

--

Building a fake TestSuite-like object:

 <a class="moz-txt-link-freetext" href="http://github.com/ctb/SomePackage/tree/master/examples/fake_suite/">http://github.com/ctb/SomePackage/tree/master/examples/fake_suite/</a>

--

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 &lt;something&gt;

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
  </pre>
</blockquote>
<br>
<br>
<pre class="moz-signature" cols="72">-- 
<a class="moz-txt-link-freetext" href="http://www.ironpythoninaction.com/">http://www.ironpythoninaction.com/</a>
<a class="moz-txt-link-freetext" href="http://www.voidspace.org.uk/blog">http://www.voidspace.org.uk/blog</a>

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 (&#8221;BOGUS AGREEMENTS&#8221;) 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.

</pre>
</body>
</html>