我有一个我想测试使用的应用程序,unittest
但我遇到了一些问题。我的目录结构如下:
root_dir
├── src
│ ├── cmds
│ │ ├── baz.py
│ │ ├── __init__.py
│ │ └── bar.py
│ └── foo.py
└── tests
├── cmds.py
└── __init__.py
我想测试baz
和bar
模块cmds
,我正在尝试做
root_dir> python2.7 -m unittest tests.cmds
但是tests.cmds
我无法在我的目录中导入cmds
包。src
我怎样才能使这项工作?
基本上我想分别从和root_dir
目录测试应用程序。src
tests
我尝试附加src
到,但是当我从中sys.path
导入时,我仍然从单元测试中得到一个。cmds.baz
tests/cmds.py
AttributeError: 'module' object has no attribute 'cmds'
编辑:我的导入和sys.path
声明是:
import sys
sys.path.append('../src')
from cmds.baz import about
和追溯:
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'