Missing package causes Test Explorer to incorrectly report missing test class in module
See original GitHub issueWhen a unittest.TestCase subclass in a module has a test method that tests functionality depending on a missing package, instead of reporting the missing package when the test is run, it reports the test class as missing:
Message: ‘module’ object has no attribute ‘Test_Foo’
This is misleading, and does not help diagnose the actual problem.
To recreate:
- Create a new python project (File->New Project->Python Application)
- Add a virtual environment for the project (Solution Exporer->Right Click on Python Environments->Add Virtual Environment
- Add a module, and to that add a function that is dependent on an additional package (do NOT add that package to the environment:
- Add a test class within that module, and add a method that calls the function dependent on the missing package
- The function appears in the Test Explorer. Run it.
- Note that rather than reporting the missing package, the Test Explorer reports the test class as missing:
`Test Name: test_do_ping Test FullName: Foo\Test_Foo.py::Test_Foo::test_do_ping Test Source: F:\Win7\Users\Matt\Desktop\PythonApplication1\PythonApplication1\Foo\Test_Foo.py : line 6 Test Outcome: Failed Test Duration: 0:00:00
Result StackTrace: File “C:\PROGRAM FILES (X86)\MICROSOFT VISUAL STUDIO\2017\COMMUNITY\COMMON7\IDE\EXTENSIONS\MICROSOFT\PYTHON\CORE\visualstudio_py_testlauncher.py”, line 269, in main for loaded_test in unittest.defaultTestLoader.loadTestsFromName(test): File “C:\Python27amd64\Lib\unittest\loader.py”, line 100, in loadTestsFromName parent, obj = obj, getattr(obj, part) AttributeError: ‘module’ object has no attribute ‘Test_Foo’ Result Message: ‘module’ object has no attribute ‘Test_Foo’ Result StandardError: Traceback (most recent call last): File “C:\PROGRAM FILES (X86)\MICROSOFT VISUAL STUDIO\2017\COMMUNITY\COMMON7\IDE\EXTENSIONS\MICROSOFT\PYTHON\CORE\visualstudio_py_testlauncher.py”, line 269, in main for loaded_test in unittest.defaultTestLoader.loadTestsFromName(test): File “C:\Python27amd64\Lib\unittest\loader.py”, line 100, in loadTestsFromName parent, obj = obj, getattr(obj, part) AttributeError: ‘module’ object has no attribute ‘Test_Foo’
`
Issue Analytics
- State:
- Created 6 years ago
- Comments:8 (5 by maintainers)
You’re right, though I’m not sure whether this is our fault or a Python bug.
Our static code analysis ignores (most) imports when determining which tests exist, and it seems that the Python 2.7
unittest
module doesn’t properly expose runtime errors.But we may be able to catch the
AttributeError
and do some extra runtime analysis to see whether the module failed to be imported. However, at this stage it’s likely that the error is already lost, so the best we can likely do is report that it failed to import for some unknown reason. (Attempting to import the module ourselves beforeunittest
does could cause all sorts of other breakage, so I don’t want to do that.)Running the file directly using “Start with/without debugging” should show the error, so perhaps we could recommend that as the next step in our error message?
We’ve decided that this issue is not a high enough priority for now, and so we are closing it to keep our tracker clean. Please reopen with new examples if it continues to be a problem in the latest update.