[Python] how to mock a function in pytest monkeypatch?

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

  1. Stack

    Stack Membro Participativo

    I am trying to mock a simple function but I get an AttributeError and have no clue on how to fix it.

    def my_function():
    return "original"

    def mock_my_function():
    return "mocked"

    def test_my_function(monkeypatch):
    monkeypatch.setattr(__name__, 'my_function', mock_my_function)
    assert my_function() == "mocked"


    What should I put in __name__'s place to make it work?

    Continue reading...

Compartilhe esta Página