Python doctest default namespace

In the teachings of my module, I would like to reference my module with a full namespace, for example:

hp.myfunc(1) 

And I would like to avoid cluttering the doctrines by writing:

  import healpy as hp 

in each of the doctrines.

If I run doctest.testmod, I know that I can use the globs keyword to provide this, and if I run the nose, I can use the setup function.

Is there any other standard way that could work with both?

+7
source share
2 answers

How do you manage doctrines (without a nose, that is)? If you cd'd in the package directory when you try to run them, you will have problems (if you are doing a full import, that is).

I was able to get a simple doctrist (with a fully qualified import), working with both nosetists and with a built-in bias. Here is my setup:

Project Structure:

 . └── mypackage ├── __init__.py └── mod.py 

Here is the contents of my mod.py file:

 """foo() providing module Example: >>> import mypackage.mod >>> mypackage.mod.foo() 'bar' """ def foo(): return "bar" 

from '.' directory (root of the project), now I can run the tests:

 $ python -m doctest -v mypackage/*.py 1 items had no tests: __init__ 0 tests in 1 items. 0 passed and 0 failed. Test passed. Trying: import mypackage.mod Expecting nothing ok Trying: mypackage.mod.foo() Expecting: 'bar' ok 1 items had no tests: mod.foo 1 items passed all tests: 2 tests in mod 2 tests in 2 items. 2 passed and 0 failed. Test passed. 

And now the media:

 $ nosetests --with-doctest . ---------------------------------------------------------------------- Ran 1 test in 0.008s OK 

If I try to run doctest from the mypackage directory, I get an error message (which, I suspect is happening in your case).

Finally, I don't think this should matter, but I am running Python 2.7.2

+3
source

I don't know about the nose, but you can use the globs argument in testmod() and testfile() .

Here's a simple module (called foobar.py), note that I am not importing os :

 #!/usr/bin/python """ >>> os.pipe <built-in function pipe> """ 

You can test the module as follows (example console):

 $ python2.7 Python 2.7.2 (default, Jun 29 2011, 11:10:00) [GCC 4.6.1] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import doctest, foobar 2 >>> doctest.testmod(foobar) ## will fail as expected because os has not been imported ********************************************************************** File "foobar.py", line 2, in foobar Failed example: os.pipe Exception raised: Traceback (most recent call last): File "/usr/lib/python2.7/doctest.py", line 1254, in __run compileflags, 1) in test.globs File "<doctest foobar[0]>", line 1, in <module> os.pipe NameError: name 'os' is not defined ********************************************************************** 1 items had failures: 1 of 1 in foobar ***Test Failed*** 1 failures. TestResults(failed=1, attempted=1) >>> import os >>> globs = {'os': os} >>> doctest.testmod(foobar, globs=globs) TestResults(failed=0, attempted=1) >>> # Win :) 

Your example should say:

 globs = {'hp': healp} 
+2
source

All Articles