I have a module applications/webapp/modules/a.py that contains a local_import to import applications/webapp/modules/b.py. I want to doctest a.py and b.py. The web2py shell with "-T" option partially works but complains with the exception pasted below after saying "Finding tests in a.py: class".
I understand that this is because the "-T" option is meant only for testing controllers. But due to local_import within a.py there is no easy way to doctest a.py outside of the web2py environment. So how should I go about refactoring the modules so that I can doctest them?
开发者_开发知识库Traceback (most recent call last):
File "web2py.py", line 19, in gluon.widget.start(cron=True) File "/home/.../web2py/gluon/widget.py", line 796, in start test(options.test, verbose=options.verbose) File "/home/.../web2py/gluon/shell.py", line 326, in test doctest_object(name, obj) File "/home/.../web2py/gluon/shell.py", line 322, in doctest_object doctest_object(attr_name, o) File "/home/.../web2py/gluon/shell.py", line 321, in doctest_object o = eval('%s.%s' % (name, attr_name), globs) File "", line 1, in NameError: name 'class' is not defined
Based on this, I made an attempt as follows. It works fine as of now but not sure if this is the best approach. In private/ folder, create a file tests.py with:
from gluon.shell import env
globals().update(env('webapp', import_models=True))
a = local_import('a')
import doctest
doctest.testmod(a, verbose=True)
Moreover, in a.py I used 'import b' instead of 'local_import(b)' as per this.
Run the tests using:
python web2py.py -S webapp --run applications/webapp/private/tests.py
精彩评论