Skip to content

Remove spy method #3198

@pared

Description

@pared

In some places, we use tests/utils/spy method to mock spied method so that we can observe how particular actions interact with the target method/function. Current implementation:

def spy(method_to_decorate):
    mock = MagicMock()

    def wrapper(self, *args, **kwargs):
        mock(*args, **kwargs)
        return method_to_decorate(self, *args, **kwargs)

    wrapper.mock = mock
    return wrapper

assumes that we are spying on some class method (by having self in wrapper args). This is not always true, and it leads to hiding first argument if we will use it to spy on function (eg file_md5) which results in trimmed call args for functions.

Example:

def test_spy(tmp_dir):
    tmp_dir.gen("file", "file content")

    from tests.utils import spy
    import dvc
    checksum_spy = spy(dvc.utils.file_md5)
    checksum_spy("file")
    assert checksum_spy.mock.call_args_list[0].args == ("file",)

should pass, while it does not because args tuple is empty.

[EDIT]
As @MrOutis noted, pytest's mocker can spy, so it would be good to remove current spy usages and replace them with mocker.spy.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugDid we break something?testingRelated to the tests and the testing infrastructure

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions