nose, doctest, and module names in subpackages

davidlmontgomery at gmail.com davidlmontgomery at gmail.com
Tue Sep 5 21:27:26 EDT 2006


I'm trying to use both nosetests and doctest with the tests
pulled out into a separate file.  My problem is that it
seems that I need to use different import statements
depending on from where I run the tests.

Here's my directory structure:
/dir
    __init__.py
    /sub
        __init__.py
        code_file.py
        test_code_file.py
        tests.txt

--- code_file.py: ---
def example_function():
    return 0

--- test_code_file.py ---
def run_tests():
    import doctest
    result=doctest.testfile("tests.txt")
    assert result[0] == 0

if __name__ == "__main__":
    run_tests()

--- tests.txt ---
>>> from code_file import example_function

With this, nosetests works when run from /dir/sub, but
the import fails if nosetests is run from /dir.

If I change tests.txt to

--- tests.txt ---
>>> from sub.code_file import example_function

(adding "sub.") then nosetests works from /dir, but not
from /dir/sub.  And in this case "python test_code_file.py"
fails when run from /dir/sub.

In general, I would like to run doctests during development
from the directory where the code lives, leading me to
prefer "from code_file import", but I would like to run
nosetests from the top of the tree, leading me to want
"from sub.code_file import".

I played around with variations like using doctest.DocFileSuite
and nosetests' --with-doctest option, but nothing I tried
helped with the basic issue of the name of the module
varying depending on the execution directory.

It seems I'm missing something very basic here.  Any tips
would be much appreciated.

(I'm running python 2.4.1 and nosetests 0.9.0)

David




More information about the Python-list mailing list