Skip to content

Print statements not showing in debug console during test debugging #899

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mgberg opened this issue Apr 11, 2022 · 4 comments
Closed

Print statements not showing in debug console during test debugging #899

mgberg opened this issue Apr 11, 2022 · 4 comments

Comments

@mgberg
Copy link

mgberg commented Apr 11, 2022

Behaviour

Expected vs. Actual

When debugging a Python file, if I run a print command from the debug console (like print("hello")), I see the result of the command in two places- both in the debug console and in the terminal the debugger is connected to. If I simply enter a string in the debug console it also returns, but it is a pain to use if there are newlines in it. A screenshot of what I see when I use the file debugger is below.
filedebugger

Within the last week or two, I have noticed that if I debug a Python test and I run a print command, the result does not appear in the console. Furthermore, when debugging tests, it does not connect to the same terminal that the Python file debugger does so I can't check the printed result there either. If I simply enter a string in the debug console it does return, but it is a pain to use if there are newlines in it. A screenshot of what I see when I use the test debugger is below.
testdebugger

I would expect that running a print command in the debug console while debugging a test would show the printed value in the debug console just as it does when debugging a file.

Steps to reproduce:

  1. Set a breakpoint in a Python test.
  2. Debug the test, either from the file or the testing pane
  3. When the code hits the breakpoint, enter print("hello") in the debug console
  4. Nothing is printed, and since the test debugger is not connected to the same terminal that the file debugger connects to, the output does not appear there either.

Diagnostic data

  • Python version (& distribution if applicable, e.g. Anaconda): 3.9.2
  • Type of virtual environment used (e.g. conda, venv, virtualenv, etc.): Poetry
  • Value of the python.languageServer setting: Pylance
User Settings


defaultLS: {"defaultLSType":"Pylance"}

downloadLanguageServer: true

envFile: "<placeholder>"

venvPath: "<placeholder>"

venvFolders: "<placeholder>"

condaPath: "<placeholder>"

pipenvPath: "<placeholder>"

poetryPath: "<placeholder>"

languageServer: "Pylance"

linting
• enabled: true
• cwd: "<placeholder>"
• Flake8Args: "<placeholder>"
• flake8Enabled: true
• flake8Path: "<placeholder>"
• lintOnSave: true
• banditArgs: "<placeholder>"
• banditEnabled: false
• banditPath: "<placeholder>"
• mypyArgs: "<placeholder>"
• mypyEnabled: false
• mypyPath: "<placeholder>"
• pycodestyleArgs: "<placeholder>"
• pycodestyleEnabled: false
• pycodestylePath: "<placeholder>"
• prospectorArgs: "<placeholder>"
• prospectorEnabled: false
• prospectorPath: "<placeholder>"
• pydocstyleArgs: "<placeholder>"
• pydocstyleEnabled: false
• pydocstylePath: "<placeholder>"
• pylamaArgs: "<placeholder>"
• pylamaEnabled: false
• pylamaPath: "<placeholder>"
• pylintArgs: "<placeholder>"
• pylintPath: "<placeholder>"

sortImports
• args: "<placeholder>"
• path: "<placeholder>"

formatting
• autopep8Args: "<placeholder>"
• autopep8Path: "<placeholder>"
• provider: "black"
• blackArgs: "<placeholder>"
• blackPath: "<placeholder>"
• yapfArgs: "<placeholder>"
• yapfPath: "<placeholder>"

testing
• cwd: "<placeholder>"
• debugPort: 3000
• nosetestArgs: "<placeholder>"
• nosetestsEnabled: false
• nosetestPath: "<placeholder>"
• promptToConfigure: true
• pytestArgs: "<placeholder>"
• pytestEnabled: true
• pytestPath: "<placeholder>"
• unittestArgs: "<placeholder>"
• unittestEnabled: false
• autoTestDiscoverOnSaveEnabled: false

terminal
• activateEnvironment: true
• executeInFileDir: "<placeholder>"
• launchArgs: "<placeholder>"

experiments
• enabled: true
• optInto: []
• optOutFrom: []

tensorBoard
• logDirectory: "<placeholder>"

@karthiknadig karthiknadig transferred this issue from microsoft/vscode-python Apr 11, 2022
@fabioz
Copy link
Collaborator

fabioz commented Apr 11, 2022

This happens because pytest captures the output.

If you don't want that behavior you can pass --capture=no in the pytest command line arguments to disable it.

-- this changed from the last version because previously we'd always capture and print the output, but on the regular case what'd happen is that the output would appear printed twice. Now we no longer do that, but this has the downside that if a tool (such as pytest) captures it nothing will be printed.

I guess we could potentially have some setting to always replay messages, but I'm not sure it's much better than disabling the pytest capture (in practice when debugging I always disable pytest capture because you're usually debugging a single test anyways and the capture in this case is just annoying).

@pabloazurduy
Copy link

+1 to this issue, some workarounds that I found

@adriangb
Copy link

adriangb commented Apr 7, 2023

If you don't want that behavior you can pass --capture=no in the pytest command line arguments to disable it.

@fabioz where does that need to be set? I've tried setting it via "python.testing.pytestArgs" in settings.json but that seems to apply to things run via the Run tab not the Tests tab. I tried setting it in the Pytest configuration (addopts = '--no-capture' in the [tool.pytest.ini_options] section ofpyproject.toml) and that doesn't work either.

@liuyang2356
Copy link

I tried setting it in my pytest.ini and it works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants