Skip to content

Debugger breaks when patching json.loads #845

Description

@ksaito1125

The code below runs correctly in pytest.
When I run the same code from VS Code's decoder, it doesn't return from the patch call.

import json


def sample(json_data):
    return json.loads(json_data)

from unittest.mock import MagicMock, patch


def test_sample():
    data = {'a1': 'b', 'a2': 'c'}
    json_data = json.dumps(data)
    with patch('json.loads', return_value='aa'): # Step execution does not come back
        assert 'aa' == sample(json_data)

def test_sample_mocker(mocker):
    data = {'a1': 'b', 'a2': 'c'}
    json_data = json.dumps(data)
    mocker.patch('json.loads', return_value='aa') # Step execution does not come back
    print('debug=' + sample(json_data))
    assert 'aa' == sample(json_data)

The execution result of pytest is good as follows.

$ pytest test_sample.py 
============================================================ test session starts =============================================================
platform linux -- Python 3.9.9, pytest-6.2.5, py-1.11.0, pluggy-1.0.0
rootdir: /workspaces/sapapi
plugins: cov-3.0.0, mock-3.7.0
collected 2 items                                                                                                                            

test_sample.py ..                                                                                                                      [100%]

============================================================= 2 passed in 0.05s ==============================================================

The execution result of VS Code is as follows.

image

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    wontfixThis will not be worked on

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions