ImportErrors getting trapped in unittest.loadTestsFromNames()

201 views
Skip to first unread message

Peter Bengtsson

unread,
Oct 17, 2010, 12:12:20 PM10/17/10
to Tornado Web Server
I've had to change my test runner slightly because if I get any
ImportErrors raised anywhere in the running code it will raise an
AttributeError. Example:
Traceback (most recent call last):
File "_run_tests.py", line 28, in <module>
tornado.testing.main()
File "/home/peterbe/virtualenvs/worklog/lib/python2.6/site-packages/
tornado/testing.py", line 320, in main
unittest.main(module=None, defaultTest='__main__.all', argv=argv)
File "/usr/lib/python2.6/unittest.py", line 816, in __init__
self.parseArgs(argv)
File "/usr/lib/python2.6/unittest.py", line 843, in parseArgs
self.createTests()
File "/usr/lib/python2.6/unittest.py", line 849, in createTests
self.module)
File "/usr/lib/python2.6/unittest.py", line 613, in
loadTestsFromNames
suites = [self.loadTestsFromName(name, module) for name in names]
File "/usr/lib/python2.6/unittest.py", line 598, in
loadTestsFromName
test = obj()
File "_run_tests.py", line 13, in all
return unittest.defaultTestLoader.loadTestsFromNames(TEST_MODULES)
File "/usr/lib/python2.6/unittest.py", line 613, in
loadTestsFromNames
suites = [self.loadTestsFromName(name, module) for name in names]
File "/usr/lib/python2.6/unittest.py", line 584, in
loadTestsFromName
parent, obj = obj, getattr(obj, part)
AttributeError: 'module' object has no attribute 'test_app'


Below is a necessary hack I had to put in so that I get the right
error exception raised. Is this something we can do anything about in
Tornado or a problem with Python 2.6's unittest?


#!/usr/bin/env python
import unittest

TEST_MODULES = [
'tests.test_app',
'tests.test_api',
'tests.test_models',
'tests.test_utils',
]

def all():
try:
return
unittest.defaultTestLoader.loadTestsFromNames(TEST_MODULES)
except AttributeError, e:
if "'module' object has no attribute 'test_app'" in str(e):
# most likely because of an import error
for m in TEST_MODULES:
__import__(m, globals(), locals())
raise


if __name__ == '__main__':
import tornado.testing
#import cProfile, pstats
#cProfile.run('tornado.testing.main()')
tornado.testing.main()

Ben Darnell

unread,
Oct 17, 2010, 3:06:49 PM10/17/10
to python-...@googlegroups.com
On Sun, Oct 17, 2010 at 9:12 AM, Peter Bengtsson <pet...@gmail.com> wrote:
> Below is a necessary hack I had to put in so that I get the right
> error exception raised. Is this something we can do anything about in
> Tornado or a problem with Python 2.6's unittest?

It's a problem with python's unittest module (or really it's a problem
with imprecise exception behavior from __import__ - you can't
distinguish between a module that doesn't exist and one that exists
but could not be loaded due to an internal ImportError). The unittest
loader wants to support loading individual classes and methods, so
when it sees an ImportError it just chops one level off the name and
looks it up later with getattr. I don't know of any workaround other
than just retrying the import outside the test loader context (as long
as you know it's supposed to be a module and not a class/method inside
the module). Python 2.7 made a lot of changes to the unittest module,
but this issue remains.

-Ben

Peter Bengtsson

unread,
Oct 19, 2010, 11:00:30 AM10/19/10
to Tornado Web Server
On Oct 17, 8:06 pm, Ben Darnell <b...@bendarnell.com> wrote:
That's fine then. I hope my little "hack" can be of use to other
people also getting weird AttributeError exceptions.

> -Ben
Reply all
Reply to author
Forward
0 new messages