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.
Steps to reproduce, following article Testing Python in Visual Studio Code for details of file content:
inc_dec.py
(see article linked above)tests
test_unittest.py
and empty file __init__.py
(see article linked above)tests
foldertest_*.py
For me, despite the settings clearly showing that unittest is enabled and pytest is not, this produces a "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:
python -m unittest discover -v -s . -p "test_*.py"
- runs the tests, but the IDE still doesn't discover them__pycache__
folderstests
folderOn 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.
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.