vscode-python: Pytest Discovery Error
Type: Bug
Behaviour
Expected vs. Actual
Expected: Discover, run and debug tests Actual: Python3 error (see output from Output panel below).
Steps to reproduce:
- Try to discover/run/debug tests
Everything works fine from the terminal using the same command (python -m pytest --collect-only tests
) from the same cwd
(./backend/
). Tests also run fine using pytest
, pytest -v
, python -m pytest
and python -m pytest tests
The project structure is as follows:
<PROJECT_ROOT>
|-backend
| |-.pytest_cache
| |-tests
| |-models
| |-__pycache__
| |-venv
| |-data
| |-services
|-frontend
|-.git
|-.vscode
The diagnostic data below shows virtual environment as Global
despite the fact that I am using a virtual environment. I have since changed the value of python.defaultInterpreterPath
to backend/venv/bin/python
and the virtual environment is now being detected and I have changed the interpreter path to use it. However, this has made no difference to test discovery.
Additionally, I’d like to add that as a workaround I have installed the Python Test Explorer extension by the Little Fox Team as this at least allows me to debug tests which is the whole reason for me wanting to run tests from VS Code. I can debug tests using the inline buttons that the aforementioned extension shows in the editor. The test explorer itself is still broken.
Diagnostic data
- Python version (& distribution if applicable, e.g. Anaconda): 3.11.4
- Type of virtual environment used (e.g. conda, venv, virtualenv, etc.): Global
- Value of the
python.languageServer
setting: Default
Output for Python
in the Output
panel (View
→Output
, change the drop-down the upper-right of the Output
panel to Python
)
2023-08-04 12:46:15.980 [error] pytest test discovery error
[Error: spawn /opt/homebrew/bin/python3 ENOENT
at ChildProcess._handle.onexit (node:internal/child_process:285:19)
at onErrorNT (node:internal/child_process:506:16)
at process.processTicksAndRejections (node:internal/process/task_queues:83:21)] {
errno: -2,
code: 'ENOENT',
syscall: 'spawn /opt/homebrew/bin/python3',
path: '/opt/homebrew/bin/python3',
spawnargs: [ '-m', 'pytest', '-p', 'vscode_pytest', '--collect-only', 'tests' ]
}
User Settings
languageServer: "Pylance"
testing
• cwd: "backend"
• pytestArgs: ["tests"]
• pytestEnabled: true
Extension version: 2023.14.0 VS Code version: Code 1.80.2 (2ccd690cbff1569e4a83d7c43d45101f817401dc, 2023-07-27T20:57:59.134Z) OS version: Darwin arm64 22.6.0 Modes:
System Info
Item | Value |
---|---|
CPUs | Apple M1 Pro (8 x 24) |
GPU Status | 2d_canvas: enabled canvas_oop_rasterization: disabled_off direct_rendering_display_compositor: disabled_off_ok gpu_compositing: enabled metal: disabled_off multiple_raster_threads: enabled_on opengl: enabled_on rasterization: enabled raw_draw: disabled_off_ok video_decode: enabled video_encode: enabled vulkan: disabled_off webgl: enabled webgl2: enabled webgpu: enabled |
Load (avg) | 4, 6, 7 |
Memory (System) | 16.00GB (0.19GB free) |
Process Argv | –crash-reporter-id 9dabde45-0274-4158-bbba-eb1f02f0cefb |
Screen Reader | no |
VM | 0% |
A/B Experiments
vsliv368cf:30146710
vsreu685:30147344
python383:30185418
vspor879:30202332
vspor708:30202333
vspor363:30204092
vslsvsres303:30308271
vserr242cf:30382550
pythontb:30283811
vsjup518:30340749
pythonptprofiler:30281270
vsdfh931:30280409
vshan820:30294714
vstes263cf:30335440
vscod805:30301674
binariesv615:30325510
bridge0708:30335490
bridge0723:30353136
vsaa593cf:30376535
pythonvs932:30410667
py29gd2263:30792226
vscaac:30438847
vsclangdf:30486550
c4g48928:30535728
dsvsc012:30540252
pynewext54:30695312
azure-dev_surveyone:30548225
vsccc:30803844
282f8724:30602487
f6dab269:30613381
showlangstatbar:30737416
vsctsb:30748421
03d35959:30757346
pythonfmttext:30731395
pythoncmv:30756943
fixshowwlkth:30771522
showindicator:30805244
pythongtdpath:30769146
i26e3531:30792625
gsofa:30804715
pythonnosmt12:30797651
pythonidxptcf:30805731
pythonnoceb:30805159
e537b577:30795824
dsvsc013:30795093
dsvsc014:30804076
About this issue
- Original URL
- State: closed
- Created a year ago
- Reactions: 1
- Comments: 42 (20 by maintainers)
Commits related to this issue
- remove node deletion for error tolerant discovery (#22207) helps with a part of https://github.com/microsoft/vscode-python/issues/21757 — committed to microsoft/vscode-python by eleanorjboyd 8 months ago
Thank you for your issue report. We are looking into this now! In the meantime, you are likely on the new testing rewrite and this is why you saw a change in behavior. You can opt out of the rewrite as I get this fix in by setting this in your user settings:
"python.experiments.optOutFrom": ["pythonTestAdapter"],
. If this doesn’t work let me know as this will mean it is a different issue. Thank you and I will send updates in this thread as I get a fix in.Hi @MaxHorwood, glad it now works! That’s weird it happened unexpectedly but let me know if it resurfaces.
@msonsona glad that upgrade worked as well! Are you not sure about the feasibility at your company you are saying as opposed to just on your own machine? Let me know if you do have any other questions, I am closing this as resolved in the meantime. Thanks!
After upgrading pytest to
7.4.3
(it’s a work-related repo, so not 100% sure about the feasibility of upgrading this on my end, but let’s try for now), I also upgraded to the pre-release version of the extension (now runningv2023.19.12981006
), and had to remove the folder argument for pytest being set originally in my.vscode/settings.json
:and now it seems to be able to discover and run tests!
thanks @eleanorjboyd for your support! 🙌
@msonsona, could you try running
pip install --upgrade pytest
then running again? We are on pytest 7.4.2 now so I am not sure if there has been changes to this specific hook but that could be an issue.In terms of a minimal repro this is just a small example project that replicates the bug. If you are happy to share your current project you could send that over so I can try it myself but since some people do not like sharing their whole projects a minimal repro could be a small project you create where you still see the bugs. For example if you create a new workspace, create a simple pytest, and run it does the bug still exist? If so then send over that simple pytest so we are talking about running the same code. Thanks!
@tboddyspargo this should fix your issue: https://github.com/microsoft/vscode-python/pull/22207
hm yes- I am seeing how this would happen. It is written that the test explorer is cleared when the Python subprocess exists with a non-0 exit code which happens when this other issue arises. Will put in a fix for this
I would like to add I’ve been having a similar issue reported here for the past few weeks. Thank you @alimbada for finding a workaround. Installing the Little Fox Team extensions worked without changing any of the VS Code settings.
@eleanorjboyd Reverting to the previous test adapter allows tests to discovered and run correctly within the test explorer (both sidebar and inline), but switching to the new test adapter results in the “0/0 tests passed” issue mentioned by @alimbada. I am running pytest with a Django project, with a few extensions installed.
The ‘OUTPUT’ tab for Python shows (includes redacted lines, happy to send these privately):
The ‘TEST RESULTS’ tab shows:
Happy to provide do more tests or provide more information, let me know what you need. I’ve reverted by to the original test adapter for the moment.
Hi @eleanorjboyd,
Thanks for your reply. I’ve actually just got test discovery and debugging working now by changing
python.testing.cwd
to${workspaceFolder}/backend
instead of justbackend
. I thought I’d tried this before but maybe the combination of settingpython.defaultInterpreterPath
tobackend/venv/bin/python
along with settingpython.testing.cwd
fixed it. Previously, I was unsure if the${workspaceFolder}
placeholder would work as it did not show up as an autocomplete option when changing that setting but a suggestion from a StackOverflow comment prompted me to try again and this time it worked.There is one outstanding issue which is that running tests without debugging shows
0/0 tests passed 0.00%
in the Test Explorer and shows the following in the output (with one client connection line per test):As mentioned in my previous post, I only need to run tests in VS Code to debug so I’m happy with the current situation as I can still run tests from the terminal and have set up a VS Code task to do so.
However, in the interest of providing more data to improve this extension I applied your suggestion of setting
python.experiments.optOutFrom
but this did not change the behaviour of running without debugging.