I have an application that I'd like to test using unittest
but I have some problems.
My directory structure is as follows:
root_dir
├── src
│?? ├── cmds
│?? │?? ├── baz.py
│?? │?? ├── __init__.py
│?? │?? └── bar.py
│?? └── foo.py
└── tests
├── cmds.py
└── __init__.py
I want to test the baz
and bar
modules from cmds
and I'm trying to do
root_dir> python2.7 -m unittest tests.cmds
But in tests.cmds
I cannot import the cmds
package in my src
dir.
How can I make this work?
Basically I want to test the application from root_dir
with the src
and tests
directories separately.
I tried appending src
to sys.path
, but when I import cmds.baz
from tests/cmds.py
I still get an AttributeError: 'module' object has no attribute 'cmds'
from unittest.
Edit:
My import and sys.path
statement is:
import sys
sys.path.append('../src')
from cmds.baz import about
And the traceback:
Traceback (most recent call last):
File "/usr/lib/python2.7/runpy.py", line 162, in _run_module_as_main
"__main__", fname, loader, pkg_name)
File "/usr/lib/python2.7/runpy.py", line 72, in _run_code
exec code in run_globals
File "/usr/lib/python2.7/unittest/__main__.py", line 12, in <module>
main(module=None)
File "/usr/lib/python2.7/unittest/main.py", line 94, in __init__
self.parseArgs(argv)
File "/usr/lib/python2.7/unittest/main.py", line 149, in parseArgs
self.createTests()
File "/usr/lib/python2.7/unittest/main.py", line 158, in createTests
self.module)
File "/usr/lib/python2.7/unittest/loader.py", line 128, in loadTestsFromNames
suites = [self.loadTestsFromName(name, module) for name in names]
File "/usr/lib/python2.7/unittest/loader.py", line 100, in loadTestsFromName
parent, obj = obj, getattr(obj, part)
AttributeError: 'module' object has no attribute 'cmds'
See Question&Answers more detail:
os