doctest not seeing any of my doc tests

  • Thread starter Steven D'Aprano
  • Start date
S

Steven D'Aprano

I have a function in my module:

def selftest(verbose=False):
import doctest
doctest.testmod(verbose=verbose)


When I run it, it fails to find any of my doc tests, including the tests
in __main__, and in fact it looks like it can't even find my functions
and classes:

1 items had no tests:
__main__
0 tests in 1 items.
0 passed and 0 failed.
Test passed.


The second and subsequent times I call selftest, I get the same result
except that it adds this line to the end:

*** DocTestRunner.merge: '__main__' in both testers; summing outcomes.


I've exited from Python and restarted the interactive interpreter. I've
deleted the module .pyc file. Neither has helped.

I've tried reading over the doctest module to see if there are any hints
as to what's going wrong, but it's quite opaque to me. If there's anyone
out there with a good understanding of doctest, I'd appreciate some
pointers for where to start looking, or otherwise how to fix this problem.

For what it's worth, I'm running

Python 2.5 (r25:51908, Apr 10 2007, 10:29:13)
[GCC 4.1.2 20070403 (Red Hat 4.1.2-8)] on linux2
 
F

Fredrik Lundh

Steven said:
I have a function in my module:

def selftest(verbose=False):
import doctest
doctest.testmod(verbose=verbose)

what happens if you change the above to

def selftest(verbose=False):
import doctest, yourmodule
doctest.testmod(yourmodule, verbose=verbose)

(where yourmodule is the name of your module, obviously)

</F>
 
B

bearophileHUGS

Steven D'Aprano, first of all you can try to run a minimal module, to
see if your doctesting works, like:

"""3
"""
def foo():
""" 1
"""
return 0
import doctest
doctest.testmod()

If that works correctly, then you can show us some more of the code
and tests.

Bye,
bearophile
 
S

Steven D'Aprano

what happens if you change the above to

def selftest(verbose=False):
import doctest, yourmodule
doctest.testmod(yourmodule, verbose=verbose)

(where yourmodule is the name of your module, obviously)



Well, that certainly fixed the problem, thanks. It now correctly runs all
my doctests.

But I don't understand why a bare doctest.testmod() works when you run it
non-interactively, but fails when run interactively.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,154
Members
46,701
Latest member
XavierQ83

Latest Threads

Top