Skip to content
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

Test execution hangs when running (by UI only) an individual test that mocks open #24399

Open
MartinSalinas98 opened this issue Nov 6, 2024 · 5 comments
Assignees
Labels
area-testing bug Issue identified by VS Code Team member as probable bug feature-request Request for new features or functionality needs community feedback Awaiting community feedback

Comments

@MartinSalinas98
Copy link

MartinSalinas98 commented Nov 6, 2024

Description

When running Python tests in VS Code using Test Adapter Converter UI, test will hang if I individually run a test that uses mock_open from unittest.

If I run the individual test from command line, it works as expected. If I run all the tests in the file, both from the UI or terminal, it also works as expected without hanging. If I run the individual test from the UI in debug mode, it also works as intended.

Note: When running the test individually from the UI, even if it hangs, the test results tab shows the expected message indicating that the test passed correctly:

Running pytest with args: ['-p', 'vscode_pytest', '--rootdir=c:\\Users\\Martin\\Documents\\Repos\\my-test-app', 'c:\\Users\\Martin\\Documents\\Repos\\my-test-app\\tests\\unitary\\my_file_test.py::test_read_file_contents']
============================= test session starts =============================
platform win32 -- Python 3.12.1, pytest-8.3.3, pluggy-1.5.0
rootdir: c:\Users\Martin\Documents\Repos\my-test-app
configfile: pyproject.toml
plugins: cov-5.0.0
collected 1 item

tests\unitary\my_file_test.py .

============================== 1 passed in 0.15s ==============================

However, the Finished running tests! line won't show up until I manually stop the test from the UI.

Enviroment

I am using Python 3.12.1, in a Poetry enviroment, with pytest 8.3.3 (latest release as of right now) and pytest-cov 5.0.0. VS Code is in version 1.95.1, and my installed extensions are:

  • Python: v2024.18.0
  • Pylance: v2024.11.1
  • Python Debugger: v2024.12.0
  • Test Adapter Converter: v0.2.1
  • YAML: v1.15.0
  • SonarLint: v.4.11.1
  • PowerShell: v2024.4.0
  • GitHub Actions: v0.27.0
  • Even Better TOML: v0.19.2

All of this is running in a Windows 10 laptop.

Example

To replicate this issue, i made this simple function:

def read_file_contents(file_path):
    with open(file_path, 'r') as file:
        return file.readlines()

With this test:

from unittest.mock import mock_open

import pytest

__STREAM_READLINES = ["line1\n", "line2\n", "line3\n"]

def test_read_file_contents(__mock_open):
  result = read_file_contents('dummy_path.txt')
  assert result == __STREAM_READLINES

@pytest.fixture
def __mock_open():
  m_open = mock_open(read_data=''.join(__STREAM_READLINES))
  with patch("builtins.open", m_open):
    yield m_open
@connor4312 connor4312 transferred this issue from microsoft/vscode-test-adapter-converter Nov 6, 2024
@github-actions github-actions bot added the triage-needed Needs assignment to the proper sub-team label Nov 6, 2024
@eleanorjboyd
Copy link
Member

will investigate- thanks

@mlataza
Copy link

mlataza commented Dec 4, 2024

I'm experiencing the same issue.

@eleanorjboyd eleanorjboyd added bug Issue identified by VS Code Team member as probable bug and removed triage-needed Needs assignment to the proper sub-team labels Dec 16, 2024
@mhuels
Copy link

mhuels commented Dec 18, 2024

I'm having the same (or a similar) issue on VSCode's version 1.95.3 without any unit test mocks. I can't reproduce the error yet, but it keeps happening quite regularly.

@eleanorjboyd
Copy link
Member

hm yes so we use open in order to communicate via fifo / named pipes (depending on machine type). This is how we send the per execution test output so it is dynamic. You can see the specific line here: https://github.com/eleanorjboyd/vscode-python/blob/af9a450eaf9dbd998a03863886c7042afa8bf289/python_files/vscode_pytest/__init__.py#L922.

There are potentially some work-arounds but are challenging / time consuming therefore I am going to turn this into a feature request to gather community interest on making this possible. Thanks

@eleanorjboyd eleanorjboyd added feature-request Request for new features or functionality needs community feedback Awaiting community feedback labels Feb 7, 2025
Copy link

github-actions bot commented Feb 7, 2025

Thanks for the feature request! We are going to give the community 60 days from when this issue was created to provide 7 👍 upvotes on the opening comment to gauge general interest in this idea. If there's enough upvotes then we will consider this feature request in our future planning. If there's unfortunately not enough upvotes then we will close this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-testing bug Issue identified by VS Code Team member as probable bug feature-request Request for new features or functionality needs community feedback Awaiting community feedback
Projects
None yet
Development

No branches or pull requests

4 participants