PTVS icon indicating copy to clipboard operation
PTVS copied to clipboard

Python unittest support in VS2022 completely broken

Open vsfeedback opened this issue 2 years ago • 2 comments

This issue has been moved from a ticket on Developer Community.


Unit tests are not found on discovery, and if they are they cannot be run. They never can be debugged.

My Python project has a structure:

  • directory code contains *.py for the application
  • directory tests contains test_*.py with tests (using unittest)

If I select "unittest" as framework and:

  1. "." as location, then the tests are not discovered
  2. "tests" as location, then the tests are discovered but cannot be run, as the VS2022 test adapter cannot load the test_*.py file.
  3. move the tests to the root of the project and use "." as location, the tests are discovered and can be run, but cannot be debugged. The debugger can be started, but the breakpoints in both the unit test and application code are ignored.

Apparently the Python unittest support in VS2022 is limited to "hello world"-type of applications.

The error message in (2) is: Failed to import test module: test_XXX Traceback (most recent call last): File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\unittest\loader.py", line 154, in loadTestsFromName module = import(module_name) ModuleNotFoundError: No module named 'test_XXX'

Standard Error:  Traceback (most recent call last): File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\unittest\case.py", line 59, in testPartExecutor yield File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\unittest\case.py", line 592, in run self._callTestMethod(testMethod) File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\unittest\case.py", line 550, in _callTestMethod method() File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\unittest\loader.py", line 34, in testFailure raise self._exception ImportError: Failed to import test module: test_XXX Traceback (most recent call last): File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\unittest\loader.py", line 154, in loadTestsFromName module = import(module_name) ModuleNotFoundError: No module named 'test_XXX'

There is a workaround for 2: add the tests directory to the search path of the Python project. But VS2022 should do that, based on the settings of unittest in the project.

There is a workaround for (3) (start test_*.py with the normal debugger) but that is a lot of work as the main application requires command line arguments and running a test requires removal of the arguments from the project)


Original Comments

Feedback Bot on 7/25/2022, 00:40 AM:

(private comment, text removed)


Original Solutions

(no solutions)

vsfeedback avatar Aug 03 '22 18:08 vsfeedback

Hi there, could you try adding an __init__.py in your tests directory and see if that works? Thanks!

image

StellaHuang95 avatar Aug 03 '22 19:08 StellaHuang95

The debugging part is a separate issue, see https://github.com/microsoft/PTVS/issues/6796

StellaHuang95 avatar Aug 03 '22 19:08 StellaHuang95

Going to close this out. If the issue still persists, please reopen with the information requested. Thanks.😊

StellaHuang95 avatar Aug 26 '22 23:08 StellaHuang95