samedi 17 octobre 2020

How to verify whether a function has been hit with pytest

I have tried this:

def test_send_confirm_hit(monkeypatch):
    hit = False
    def called():
        global hit
        hit = True

    monkeypatch.setattr("web.email.send_confirm", called)

    # ... some event that will cause web.email.send_confirm to be hit

    assert hit  # verify send_confirm was hit

Which appears to work although I would rather not use a global variable. What is the best way to do this?

Aucun commentaire:

Enregistrer un commentaire