Search code examples
visual-studio-codepython-unittest

Visual Studio Code triggers use of pytest when configured for unittest


I've successfully used the standard python unittest framework in several projects recently, but in the last few days it fails to work as before. One issue is that it now seems to try to use pytest instead in test discovery in brand new projects, despite my never having used pytest.

  • VS Code versions tried: 1.80.0, 1.79.2 and 1.79.1 (user setup in each case)
  • Python extension: v2023.12.0
  • Python versions tried: 3.9.13, 3.10.6, 3.10.11
  • Windows 10 Version 22H2 (OS Build 19045.3086)

Steps to reproduce, following article Testing Python in Visual Studio Code for details of file content:

  1. Create new folder
  2. Open folder in VS Code
  3. In VS Code, create file inc_dec.py (see article linked above)
  4. Ensure a vanilla install of python 3.9+ is being used as the environment (see list above for those I tried)
  5. Add folder tests
  6. Within that, create file test_unittest.py and empty file __init__.py (see article linked above)
  7. Configure Python Tests (see article linked above), with the following choices:
    1. Standard python unittest framework
    2. tests folder
    3. pattern test_*.py

For me, despite the settings clearly showing that unittest is enabled and pytest is not, this produces a "Pytest Discovery Error" Pytest Discovery Error and the Python OUTPUT log also indicates this e.g.

2023-07-14 13:08:29.745 [info] Running discovery for unittest using the new test adapter.
2023-07-14 13:08:29.771 [info] > C:\Python\3.10.11\python.exe -m pytest -p vscode_pytest --collect-only

I was expecting tests to be discovered (and ultimately be debuggable).

Things I have tried include:

  • stop and restart VS Code
  • reboot
  • revert to a previous version of VS Code (as listed above)
  • different base versions of python (as listed above)
  • virtual environments that have previously worked in other projects
  • PyCharm - works ok there but can't use due to Jupyter Notebook requirements
  • running python -m unittest discover -v -s . -p "test_*.py" - runs the tests, but the IDE still doesn't discover them
  • deleting __pycache__ folders
  • dispensing with a tests folder

On a side note, existing projects still discover tests but in VS Code they no longer hit breakpoints when debugging tests, and in running tests they now complain about source files which are referenced in a sub-folder (I've seen several suggestions about fixing that, none I've seen work for me). This may be unrelated to the issue I'm logging now, but mentioning here in case it impacts proposed suggestions. Tests and test debugging work in PyCharm Community Edition for both the test code above and existing projects, but unfortunately that edition does not support running and debugging Jupyter Notebooks, which we need. Break points are hit when debugging other python files.


Solution

  • This may end up being a short term issue. Two workarounds fix it for me, as per comments in GitHub issue VS Code python unittest fails to import modules outside tests folder.

    A. Revert the version of Python extension in VSCode from v2023.12.0 to v2023.8.0.

    B. Insert setting "python.experiments.optOutFrom": ["pythonTestAdapter"] into the User Settings (not the Workspace Settings). Be mindful of trailing commas.

    I can now debug python unittest tests.

    Update

    The bug that led to this issue was fixed in Python extension v2023.19.