Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

If some test module fails to import another module unittest reports a very misleading message #58140

Closed
sbarthelemy mannequin opened this issue Feb 3, 2012 · 2 comments
Labels
stdlib Python modules in the Lib dir tests Tests in the Lib/test dir

Comments

@sbarthelemy
Copy link
Mannequin

sbarthelemy mannequin commented Feb 3, 2012

BPO 13932
Nosy @ezio-melotti, @merwok, @voidspace

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2012-02-05.01:10:04.073>
created_at = <Date 2012-02-03.15:50:43.986>
labels = ['tests', 'library']
title = 'If some test module fails to import another module unittest reports a very misleading message'
updated_at = <Date 2012-02-05.01:10:04.071>
user = 'https://bugs.python.org/sbarthelemy'

bugs.python.org fields:

activity = <Date 2012-02-05.01:10:04.071>
actor = 'michael.foord'
assignee = 'none'
closed = True
closed_date = <Date 2012-02-05.01:10:04.073>
closer = 'michael.foord'
components = ['Library (Lib)', 'Tests']
creation = <Date 2012-02-03.15:50:43.986>
creator = 'sbarthelemy'
dependencies = []
files = []
hgrepos = []
issue_num = 13932
keywords = []
message_count = 2.0
messages = ['152525', '152652']
nosy_count = 4.0
nosy_names = ['ezio.melotti', 'eric.araujo', 'michael.foord', 'sbarthelemy']
pr_nums = []
priority = 'normal'
resolution = 'duplicate'
stage = None
status = 'closed'
superseder = None
type = None
url = 'https://bugs.python.org/issue13932'
versions = ['Python 2.7']

@sbarthelemy
Copy link
Mannequin Author

sbarthelemy mannequin commented Feb 3, 2012

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'

@sbarthelemy sbarthelemy mannequin added the stdlib Python modules in the Lib dir label Feb 3, 2012
@terryjreedy terryjreedy added the tests Tests in the Lib/test dir label Feb 4, 2012
@voidspace
Copy link
Contributor

This is a duplicate of bpo-7559. See the discussion there.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir tests Tests in the Lib/test dir
Projects
None yet
Development

No branches or pull requests

2 participants