I observe a strange behaviour with Python 3 unittest. Following Testcase tests in function testValue a module that does not exist.
import sys
import unittest
class ModuleTest(unittest.TestCase):
def testValue(self):
import unknown_module
result = unknown_module.value
self.assertEqual(0.0, result)
if __name__ == "__main__":
print(sys.version)
unittest.main()
Python2 gives correctly following output:
2.7.5 (default, May 15 2013, 22:44:16) [MSC v.1500 64 bit (AMD64)]
E
======================================================================
ERROR: testValue (__main__.ModuleTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\bin\WinPython-64bit-2.7.5.1\workspace\unknown_module_test.py", line 7, in testValue
import unknown_module
ImportError: No module named unknown_module
----------------------------------------------------------------------
Ran 1 test in 0.000s
FAILED (errors=1)
But Python 3 claims an AttributeError when the unknown_module.value is referenced.
3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 10:55:48) [MSC v.1600 32 bit (Intel)]
E
======================================================================
ERROR: testValue (__main__.ModuleTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "unknown_module_test.py", line 8, in testValue
result = unknown_module.value
AttributeError: 'module' object has no attribute 'value'
----------------------------------------------------------------------
Ran 1 test in 0.016s
FAILED (errors=1)
Why doesn't throw Python 3 an ImportError as Python 2 does?
ImportError. What is the real name of your unknown module? Are you 100% certain that you didn't find a module added to Python 3 you did not know about? What doesprint(unknown_module.__file__)tell you was imported?unknown_moduleexists somewhere in Python 3.3 module search path. (not in Python 2.7 module path).AttributeError: 'module' object has no attribute '__file__'.print(unknown_module.__name__)give? Or better still, give usprint(dir(unknown_module)).unknown_moduleand['__doc__', '__initializing__', '__loader__', '__name__', '__package__', '__path__']on the module search path. I found out that I have an empty directoryunknown_module, but there is no file__init__.pyin it. But this is still mandatory to declare a package inPython 3, isn't it?