So importieren Sie den src aus dem Testmodul in Python
Ich habe eine Anwendung, die ich gerne testen möchteunittest
aber ich habe einige probleme. Meine Verzeichnisstruktur ist wie folgt:
<code>root_dir ├── src │ ├── cmds │ │ ├── baz.py │ │ ├── __init__.py │ │ └── bar.py │ └── foo.py └── tests ├── cmds.py └── __init__.py </code>
Ich möchte das testenbaz
undbar
Module auscmds
und ich versuche es zu tun
root_dir> python2.7 -m unittest tests.cmds
Aber intests.cmds
Ich kann das nicht importierencmds
Paket in meinemsrc
dir.
Wie kann ich das schaffen?
Grundsätzlich möchte ich die Anwendung von testenroot_dir
mit demsrc
undtests
Verzeichnisse getrennt.
Ich habe versucht, anzufügensrc
zusys.path
, aber wenn ich importierecmds.baz
vontests/cmds.py
Ich bekomme noch eineAttributeError: 'module' object has no attribute 'cmds'
von unittest.
Bearbeiten: Mein Import undsys.path
Aussage ist:
<code>import sys sys.path.append('../src') from cmds.baz import about </code>
Und der Traceback:
<code>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' </code>