Running a python script with nose.run(...) from outside of the script's directory results in AttributeError: 'module' object has no attribute 'tests' -
i have python application few sub directories. each subdirectory has own tests.py
file.
i use nose run of unittests across of these files in 1 shot, creating script run_unit_tests.py
calls nose.run(...)
.
if inside of directory containing run_unit_tests.py
, works fine. however, if anywhere else on file system, fails attributeerror: 'module' object has no attribute 'tests'.
here similar directory structure:
myapp/ foo/ __init__.py tests.py bar/ __init__.py tests.py run_unit_tests.py
in run_unit_tests.py
:
class myplugin(plugin): ... if __name__ == '__main__': nose.run(argv=['', 'foo.tests', '--with-my-plugin']) nose.run(argv=['', 'foo.bar.tests', '--with-my-plugin'])
if run run_unit_tests.py
while inside top myapp
directory, works fine.
however, if run script while in other folder on file system, fails with:
====================================================================== error: failure: attributeerror ('module' object has no attribute 'tests') ---------------------------------------------------------------------- traceback (most recent call last): file "/apps/python/lib/python2.7/site-packages/nose/loader.py", line 407, in loadtestsfromname module = resolve_name(addr.module) file "/apps/python/lib/python2.7/site-packages/nose/util.py", line 322, in resolve_name obj = getattr(obj, part) attributeerror: 'module' object has no attribute 'tests'
in fact, if add following run_unit_tests.py
, works fine:
import os os.chdir('/path/to/myapp')
what can change inside of nose script such can run script outside of directory?
actually, want careful here. because, reason why happening because imports in test respect to: /path/to/myapp
.
so, when run tests working directory, unit test files importing respect directory being project source. if change directories , run location, becomes root, , imports surely fail.
this bring different opinions, make sure sources referenced same project root. if here:
myapp/ foo/ __init__.py tests.py bar/ __init__.py tests.py run_unit_tests.py
i run within myapp
furthemore, consider creating tests
directory , putting tests in directory, making imports easier manage , better segregating code. however, opinion, please don't feel necessity. whatever works you, go it.
hope helps.
Comments
Post a Comment