This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: If some test module fails to import another module unittest reports a very misleading message
Type: Stage:
Components: Library (Lib), Tests Versions: Python 2.7
process
Status: closed Resolution: duplicate
Dependencies: Superseder:
Assigned To: Nosy List: eric.araujo, ezio.melotti, michael.foord, sbarthelemy
Priority: normal Keywords:

Created on 2012-02-03 15:50 by sbarthelemy, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (2)
msg152525 - (view) Author: Sébastien Barthélémy (sbarthelemy) Date: 2012-02-03 15:50
If some test module (say, testmath) fails to import some other module, unittest reports a very misleading message:

  AttributeError: 'module' object has no attribute 'testmath'

Would it be possible improve the message or, better, to simply make the test as failed. (Maybe be by inspecting the ImportError exception message).

Consider the following example (and notice the typo at "import mathhh"):

mkdir -p test
touch test/__init__.py
cat > test/testmath.py <<EOL
import unittest
import mathhh
class MathTestCase(unittest.TestCase):
    def testSin(self):
        self.assertEqual(math.sin(0), 0)
EOL
python -m unittest test.testmath

it returns 

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 'testmath'
msg152652 - (view) Author: Michael Foord (michael.foord) * (Python committer) Date: 2012-02-05 01:10
This is a duplicate of issue 7559. See the discussion there.
History
Date User Action Args
2022-04-11 14:57:26adminsetgithub: 58140
2012-02-05 01:10:04michael.foordsetstatus: open -> closed
resolution: duplicate
messages: + msg152652
2012-02-04 08:05:22eric.araujosetnosy: + eric.araujo
2012-02-04 03:44:04terry.reedysetnosy: + ezio.melotti, michael.foord
components: + Tests
2012-02-03 15:50:43sbarthelemycreate