microsoft / vscode-python-debugger

Python debugger (debugpy) extension for VS Code.
https://marketplace.visualstudio.com/items?itemName=ms-python.debugpy
MIT License
67 stars 25 forks source link

Test discovery failure / regression for uniitest #493

Open bigjools opened 2 weeks ago

bigjools commented 2 weeks ago

Behaviour

Similar to the issue reported here https://github.com/microsoft/vscode-python/issues/16593 and somewhat in here https://github.com/microsoft/vscode-python/issues/17641#issuecomment-1953338728

Test discovery is crashing (see output below). This looks like it's the old problem of syspath being wrong and it is trying to import a local module called "logging" instead of the system one. This appears to be a regression, is it was working previously and nothing else has changed for me other than upgrading vscode and extensions.

Steps to reproduce:

  1. Create a repo with its own logging.py file
  2. Configure unittest as test runner
  3. Start the debugger and watch it fail to discover the tests

Diagnostic data

settings.json test section:

    "python.testing.unittestEnabled": true,
    "python.testing.unittestArgs": [
        "-v",
        "-s",
        "helios/tests",
        "-t",
        "${workspaceFolder}",
        "-p",
        "test*.py"
    ],

Debugger output:

2024-10-21 10:38:59.984 [info] shell: bash
2024-10-21 10:39:00.014 [info] Discovering unittest tests for workspace /home/juledwar/src/helios with arguments: /home/juledwar/.vscode/extensions/ms-python.python-2024.16.1-linux-x64/python_files/unittestadapter/discovery.py,--udiscovery,-v,-s,helios/tests,-t,/home/juledwar/src/helios,-p,test*.py

2024-10-21 10:39:00.014 [info] > ./.hatch/bin/python ~/.vscode/extensions/ms-python.python-2024.16.1-linux-x64/python_files/unittestadapter/discovery.py --udiscovery -v -s helios/tests -t . -p test*.py
2024-10-21 10:39:00.014 [info] cwd: .
2024-10-21 10:39:00.230 [error] Unittest test discovery error for workspace:  /home/juledwar/src/helios 
 Failed to import test module: helios.tests.anchore.test_client
Traceback (most recent call last):
  File "/usr/lib/python3.12/unittest/loader.py", line 394, in _find_test_path
    module = self._get_module_from_name(name)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/unittest/loader.py", line 337, in _get_module_from_name
    __import__(name)
  File "/home/juledwar/src/helios/helios/tests/anchore/test_client.py", line 5, in <module>
    from unittest import mock
  File "/usr/lib/python3.12/unittest/mock.py", line 26, in <module>
    import asyncio
  File "/usr/lib/python3.12/asyncio/__init__.py", line 8, in <module>
    from .base_events import *
  File "/usr/lib/python3.12/asyncio/base_events.py", line 18, in <module>
    import concurrent.futures
  File "/usr/lib/python3.12/concurrent/futures/__init__.py", line 8, in <module>
    from concurrent.futures._base import (FIRST_COMPLETED,
  File "/usr/lib/python3.12/concurrent/futures/_base.py", line 43, in <module>
    LOGGER = logging.getLogger("concurrent.futures")
             ^^^^^^^^^^^^^^^^^
AttributeError: module 'logging' has no attribute 'getLogger'
eleanorjboyd commented 2 weeks ago

what python version do you have? We haven't made any changes in this area recently (edit I see now it says 3.12)

eleanorjboyd commented 2 weeks ago

What happens if you try and run unittest from the command line with the same arguments you have in your settings? Can you also try looking at your python path variable from the terminal vs the vscode run? I am thinking something in your path might not be showing up right as you are looking for a custom logger file

bigjools commented 2 weeks ago

python3 -m unittest discover -v -s helios/tests -t . -p 'test*.py' in the terminal works just fine. Can you remind me how to work out what sys.path it would be using in its debugger runs?

Thanks.

eleanorjboyd commented 2 weeks ago

if you add

import sys
print(sys.path)

at the very top of your file, before it attempts this import Failed to import test module: helios.tests.anchore.test_client, which I think is caused by this import: File "/home/juledwar/src/helios/helios/tests/anchore/test_client.py", line 5, in <module> from unittest import mock

bigjools commented 2 weeks ago

That first module looks mighty suspicious.

2024-10-29 08:56:44.240 [info] ['helios', '/home/juledwar/.vscode/extensions/ms-python.python-2024.16.1-linux-x64/python_files/unittestadapter/lib/python', '/home/juledwar/.vscode/extensions/ms-python.python-2024.16.1-linux-x64/python_files/unittestadapter', '/home/juledwar/src/helios/.hatch/lib/python3.12/site-packages/_pdbpp_path_hack', '/usr/lib/python312.zip', '/usr/lib/python3.12', '/usr/lib/python3.12/lib-dynload', '/home/juledwar/src/helios/.hatch/lib/python3.12/site-packages', '/home/juledwar/src/helios', '/home/juledwar/src/python-heliosclient', '/home/juledwar/.vscode/extensions/ms-python.python-2024.16.1-linux-x64/python_files/unittestadapter', '/home/juledwar/.vscode/extensions/ms-python.python-2024.16.1-linux-x64/python_files/unittestadapter', '/home/juledwar/.vscode/extensions/ms-python.python-2024.16.1-linux-x64/python_files', '/home/juledwar/.vscode/extensions/ms-python.python-2024.16.1-linux-x64/python_files/lib/python', '/home/juledwar/.vscode/extensions/ms-python.python-2024.16.1-linux-x64/python_files', '/home/juledwar/.vscode/extensions/ms-python.python-2024.16.1-linux-x64/python_files/lib/python']