question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Pytest discovery fails if something is printed to stderr

See original GitHub issue

Can I get any help with pytest discovery failure?

> /usr/local/bin/python ~/.vscode-server/extensions/ms-python.python-2022.0.1786462952/pythonFiles/testing_tools/run_adapter.py discover pytest -- --rootdir . -s --cache-clear
cwd: .
[ERROR 2022-1-3 23:36:9.283]: Error discovering pytest tests:
 [r [Error]: Graceful shutdown completed in 0.000 seconds.

	at ChildProcess.<anonymous> (/root/.vscode-server/extensions/ms-python.python-2022.0.1786462952/out/client/extension.js:32:39235)
	at Object.onceWrapper (events.js:422:26)
	at ChildProcess.emit (events.js:315:20)
	at maybeClose (internal/child_process.js:1048:16)
	at Process.ChildProcess._handle.onexit (internal/child_process.js:288:5)]

Tried many different options with “python.testing.cwd” and “python.testing.pytestArgs” but without luck.

I used unit tests in the past and they work fine in VS Code.

Also I use VS Code with “Remote Container” and work inside the Docker container and run tests inside it.

Hope that you can solve this issue soon, because with pytest it doesn’t work for me. And I have no clear vision why. Thanks.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:7
  • Comments:30

github_iconTop GitHub Comments

7reactions
calebhocommented, Feb 4, 2022

Getting a similar error, however it seems like it’s because the environment location is messed up

> conda run -n onedevex-dev --no-capture-output python ~/.vscode-server/extensions/ms-python.python-2022.0.1786462952/pythonFiles/get_output_via_markers.py ~/.vscode-server/extensions/ms-python.python-2022.0.1786462952/pythonFiles/testing_tools/run_adapter.py discover pytest -- --rootdir . -s --cache-clear .
cwd: .
[ERROR 2022-1-4 19:17:3.378]: Error discovering pytest tests:
 [r [Error]: 
EnvironmentLocationNotFound: Not a conda environment: /private/home/calebh/miniconda3/envs/onedevex-dev/envs/onedevex-dev


	at ChildProcess.<anonymous> (/private/home/calebh/.vscode-server/extensions/ms-python.python-2022.0.1786462952/out/client/extension.js:32:39235)
	at Object.onceWrapper (events.js:422:26)
	at ChildProcess.emit (events.js:315:20)
	at maybeClose (internal/child_process.js:1048:16)
	at Process.ChildProcess._handle.onexit (internal/child_process.js:288:5)
2reactions
ianhellecommented, Feb 5, 2022

I’m getting a very similar issue - pytest discovery not working in vscode while pytest from cmd line works fine. From the code Python output I see:

> f:\anaconda\condabin\conda.bat run -p F:\anaconda\envs\msticpy --no-capture-output python ~\.vscode\extensions\ms-python.python-2022.1.1779589815-dev\pythonFiles\get_output_via_markers.py ~\.vscode\extensions\ms-python.python-2022.1.1779589815-dev\pythonFiles\testing_tools\run_adapter.py discover pytest -- --rootdir . -s --cache-clear tests

Not sure if something in conda is able to interpret ~\.vscode\extens... as %userprofile%\.vscode\extens... (I’m running on Windows where “~” doesn’t resolve to anything).

If I try to copy that command (and replace the “~” with userprofile path), it fails as follows

(I’ve added -v flags to conda run to try to get more info):

(msticpy) E:\src\microsoft\msticpy>f:\anaconda\condabin\conda.bat run -v -v -v -p F:\anaconda\envs\msticpy --no-capture-output python %userprofile%\.vscode\extensions\ms-python.python-2022.1.1779589815-dev\pythonFiles\get_output_via_markers.py %userprofile%\.vscode\extensions\ms-python.python-2022.1.1779589815-dev\pythonFiles\testing_tools\run_adapter.py discover pytest -- --rootdir . -s --cache-clear tests 
DEBUG conda.gateways.logging:set_verbosity(231): verbosity set to 3
DEBUG conda.gateways.subprocess:subprocess_call(71): executing>> C:\WINDOWS\system32\cmd.exe /d /c F:\anaconda\envs\msticpy\.tmpzij4q0j9.bat
>>>PYTHON-EXEC-OUTPUT
usage: C:\Users\Ian\.vscode\extensions\ms-python.python-2022.1.1779589815-dev\pythonFiles\testing_tools\run_adapter.py [-h] {discover} ...
C:\Users\Ian\.vscode\extensions\ms-python.python-2022.1.1779589815-dev\pythonFiles\testing_tools\run_adapter.py: error: argument cmd: invalid choice: 'C:\\Users\\Ian\\.vscode\\extensions\\ms-python.python-2022.1.1779589815-dev\\pythonFiles\\testing_tools\\run_adapter.py' (choose from 'discover')
TRACE conda.gateways.subprocess:subprocess_call(108): $ C:\WINDOWS\system32\cmd.exe /d /c F:\anaconda\envs\msticpy\.tmpzij4q0j9.bat
==> cwd: E:\src\microsoft\msticpy <==
==> exit code: 2 <==
==> stdout <==
None
==> stderr <==
None

ERROR conda.cli.main_run:execute(33): Subprocess for 'conda run ['python', 'C:\\Users\\Ian\\.vscode\\extensions\\ms-python.python-2022.1.1779589815-dev\\pythonFiles\\get_output_via_markers.py', 'C:\\Users\\Ian\\.vscode\\extensions\\ms-python.python-2022.1.1779589815-dev\\pythonFiles\\testing_tools\\run_adapter.py', 'discover', 'pytest', '--', '--rootdir', '.', '-s', '--cache-clear', 'tests']' command failed.  (See above for error)
TRACE conda.gateways.disk.delete:rm_rf(165): rm_rf F:\anaconda\envs\msticpy\.tmpzij4q0j9.bat

I also tried removing the get_output_via_markers.py component of the command - still no success. But I noticed that it seemed to be trying to execute pytest from the conda base environment rather than the current conda env.

(msticpy) E:\src\microsoft\msticpy>f:\anaconda\condabin\conda.bat run -v -v -v -p F:\anaconda\envs\msticpy --no-capture-output python %userprofile%\.vscode\extensions\ms-python.python-2022.1.1779589815-dev\pythonFiles\testing_tools\run_adapter.py discover pytest -- --rootdir . -s --cache-clear tests
DEBUG conda.gateways.logging:set_verbosity(231): verbosity set to 3
DEBUG conda.gateways.subprocess:subprocess_call(71): executing>> C:\WINDOWS\system32\cmd.exe /d /c F:\anaconda\envs\msticpy\.tmp39rluiwl.bat
equivalent command: f:\anaconda\python.exe -m pytest --collect-only --rootdir . -s --cache-clear tests
TRACE conda.gateways.subprocess:subprocess_call(108): $ C:\WINDOWS\system32\cmd.exe /d /c F:\anaconda\envs\msticpy\.tmp39rluiwl.bat
==> cwd: E:\src\microsoft\msticpy <==
==> exit code: 120 <==
==> stdout <==
None
==> stderr <==
None

ERROR conda.cli.main_run:execute(33): Subprocess for 'conda run ['python', 'C:\\Users\\Ian\\.vscode\\extensions\\ms-python.python-2022.1.1779589815-dev\\pythonFiles\\testing_tools\\run_adapter.py', 'discover', 'pytest', '--', '--rootdir', '.', '-s', '--cache-clear', 'tests']' command failed.  (See above for error)
TRACE conda.gateways.disk.delete:rm_rf(165): rm_rf F:\anaconda\envs\msticpy\.tmp39rluiwl.bat

This is the line I’m referring to - although not sure if this is a anything to do with the problem - it should be f:\anaconda\envs\msticpy\python.exe

equivalent command: f:\anaconda\python.exe -m pytest --collect-only --rootdir . -s --cache-clear tests

The result is the same with or without the -v flags.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can pytest throw stderr on failure? - python
So basically I want pytest to throw anything in stderr when it's done if any test has failed. A solution I've found is...
Read more >
How to capture stdout/stderr output
During test execution any output sent to stdout and stderr is captured. If a test or a setup method fails its according captured...
Read more >
doctest — Test interactive Python examples
If you run example.py directly from the command line, doctest works its magic: ... are printed to stdout, and the final line of...
Read more >
Python Unit Testing - LSST DM Developer Guide
The pytest scheme for discovering tests inside Python modules is much more flexible ... If a test command fails, that output is renamed...
Read more >
Testing Python in Visual Studio Code
Unit tests are concerned only with the unit's interface—its arguments and ... If discovery fails (for example, the test framework isn't installed or...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found