[TIP] Fwd: Nose and doctests in extension modules...

jason pellerin jpellerin at gmail.com
Fri Jun 20 07:15:28 PDT 2008

Looks like I (again!) failed to send a reply to the whole list. Go me!

Anyway, to summarize in case you don't want to read my whole spiel:
the problem is less with nose than with doctest itself. A new
DocTestFinder subclass will be needed that also looks for docstrings
in builtin functions, since that's what primes.primes is; just telling
nose to look in primes will not help without that. I'll be happy to
accept a patch to nose that implements such a subclass. In fact, I
seem to recall that Titus has commit rights, so he can cut a branch
and get right to work, and if you guys work fast enough it can be in
0.11. :)


---------- Forwarded message ----------
From: jason pellerin <jpellerin at gmail.com>
Date: Thu, Jun 19, 2008 at 8:47 PM
Subject: Re: [TIP] Nose and doctests in extension modules...
To: Fernando Perez <fperez.net at gmail.com>

On Thu, Jun 19, 2008 at 8:04 PM, Fernando Perez <fperez.net at gmail.com> wrote:
> Howdy,
> On Thu, Jun 19, 2008 at 2:30 PM, Kumar McMillan\
> So what I'd like to know is if nose can be taught (or if it knows
> already and I'm doing something wrong) to correctly introspect objects
> that live in extension modules for their docstrings, so it can fetch
> doctests from either python sources or extension modules.

There are a couple of hurdles in your way, I'm afraid. The easy one is
to train nose's doctest plugin how to discover .so files and try to
load doctests from the modules they contain. The hard one is writing a
new DocTestFinder that can find the tests:

pbook:primes jhp$ python
Python 2.5.1 (r251:54863, Jan 17 2008, 19:35:16)
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import doctest
>>> import primes
>>> f = doctest.DocTestFinder()
>>> f._verbose = 1
>>> f.find(primes)
Finding tests in primes
>>> primes.__dict__
{'__builtins__': <module '__builtin__' (built-in)>, '__name__':
'primes', 'primes': <built-in function primes>, '__file__':
'primes.so', '__doc__': None}
>>> import inspect
>>> inspect.isfunction(primes.primes)
>>> type(primes.primes)
<type 'builtin_function_or_method'>

There's the problem: primes.primes isn't a function, it's a
builtin_function_or_method, and so doctest's finder doesn't find it.
So even when you tell nose explicitly to load tests from the primes
module (which you can do easily: nosetests --with-doctest primes) you
won't get anything.

> But numpy/scipy have gobs of extension code, and adequate
> support for nose finding doctests in extension code is really a huge
> issue for us in the long run.

I'll happily take a patch that overrides doctest's default finder for
nose, so long as it's compatible with all supported versions of python
(2.3-25 and jython). But my advice would be to skip the bother and put
the doctests in separate text files rather than in the extension
modules. Sorry.


More information about the testing-in-python mailing list