8

I've started integrating doctests into my modules. (Hooray!) These tend to be files which started as scripts, and are now are a few functions with CLI apps in the __name__=='__main__', so I don't want to put the running of the tests there. I tried nosetests --with-doctest, but get lots of failures I don't want to see, because during test discovery this import modules which don't contain doctests but do require importing things I don't have installed on this system, or should be run within special python installations. Is there a way I can run just all of my doctests?

I've considered a hotkey in vim to run "import doctest; doctest.testfile(currentFilename)" to run my doctests in the current module, and another script that runs all the tests - what do other doctest users do? Or should I be using something other than doctest?

Thomas
  • 6,515
  • 1
  • 31
  • 47

2 Answers2

6

You can also create unittests that wrap desired doctests modules, it is a native feature of doctests: http://docs.python.org/2/library/doctest.html#unittest-api.

import unittest
import doctest 
import my_module_with_doctests

def load_tests(loader, tests, ignore):
    tests.addTests(doctest.DocTestSuite(my_module_with_doctests))
    return tests
sandstrom
  • 14,554
  • 7
  • 65
  • 62
jb.
  • 23,300
  • 18
  • 98
  • 136
3

I think nose is the way. You should either exclude the problematic modules explicitly with -e or catch the missing imports in your code with constructs like this:

try:
    import simplejson as json
except ImportError:
    import json

Update:

Another option is to provide mock replacements for the missing modules. Let's say your code has something like this:

import myfunkymodule

and you're trying run your tests in a system where myfunkymodule is missing. You could create a mock_modules/myfunkymodule.py file with mock implementations of the stuff you need from it (perhaps using MiniMock, which I highly recommend if you are using doctest). You could then run nose like this:

$ PYTHONPATH=path_to/mock_modules nosetests --with-doctest
Martin Blech
  • 13,135
  • 6
  • 31
  • 35
  • 1
    I'm having trouble using -e to exclude even looking for the doctests, in certain modules, which is when the import statements are encountered. How is -e normally used? The regex to exclude the tests apparently can't just be the module name. – Thomas Sep 06 '10 at 19:38
  • 1
    True, `-e` won't help in your case. Still, `nosetests` is the way for test discovery in python AFAIK. You should go for the `try/except ImportError` approach, or else provide mock implementations for your missing modules somewhere in your `PYTHONPATH` (see my last edit). – Martin Blech Sep 07 '10 at 16:54
  • Thanks, I'll look into this. I may end up hacking a script that greps for the `>>>` interactive prompt inside of a docstring, and just runs `doctest.testfile(file)` on those modules instead. But for a more complete testing solution, what you suggest sounds better. – Thomas Sep 08 '10 at 03:38