Opened 9 years ago

Closed 8 years ago

#25542 closed Cleanup/optimization (wontfix)

Discover runner will raise AttributeError when test modules raise ImportError

Reported by: Hiroki Kiyohara Owned by: nobody
Component: Testing framework Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

When test modules raise ImportError, the discover runner will raise AttributeError.
It's difficult to detect the error reason.

I added a patch to check this problem.

ERROR: test_import_error_in_tests_module (test_runner.test_discover_runner.DiscoverRunnerTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/hirokiky/dev/django/django/tests/test_runner/test_discover_runner.py", line 172, in test_import_error_in_tests_module
    ["test_discovery_sample.tests_discovery_import_error"],
  File "/home/hirokiky/dev/django/django/django/test/runner.py", line 422, in build_suite
    tests = self.test_loader.loadTestsFromName(label)
  File "/usr/lib/python2.7/unittest/loader.py", line 100, in loadTestsFromName
    parent, obj = obj, getattr(obj, part)
AttributeError: 'module' object has no attribute 'tests_discovery_import_error'

Attachments (1)

25542.diff (1.4 KB ) - added by Hiroki Kiyohara 9 years ago.

Download all attachments as: .zip

Change History (6)

by Hiroki Kiyohara, 9 years ago

Attachment: 25542.diff added

comment:1 by Tim Graham, 9 years ago

Has patch: unset

Not sure it can be solved or that it's Django's problem. From a python-tornado thread:

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.

comment:2 by Alexander Schepanovski, 9 years ago

This is very confusing behavior. I think it should be fixed even with a hack, like introspecting traceback.

comment:3 by Tim Graham, 9 years ago

Triage Stage: UnreviewedAccepted
Type: BugCleanup/optimization

Accepting on the basis for further investigation.

comment:4 by Chris Jerdonek, 8 years ago

I believe this issue corresponds to this Python issue, which is reported to be fixed in Python 3.5's changelog.

comment:5 by Tim Graham, 8 years ago

Resolution: wontfix
Status: newclosed

I agree with that.

Note: See TracTickets for help on using tickets.
Back to Top