[Python] How to patch a function with pytest-mock regardless of path/namespace used to call it?

Discussão em 'Python' iniciado por Stack, Junho 20, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to mock a function regardless of how it is imported in the code. I can't find info on how to do it.

    Here is an example, the first assert works as expected but the second fails because path:

    import mymodule
    from mymodule import test1


    def test_mock(mocker):
    mocker.patch('mymodule.test1', return_value="mocked")
    assert mymodule.test1() == "mocked" # works
    assert test1() == "mocked" # fails

    Continue reading...

Compartilhe esta Página