Skip to content

Instantly share code, notes, and snippets.

@ilyasotkov
Created March 18, 2022 09:53
Show Gist options
  • Save ilyasotkov/20207f7b83de84b5b19d22eac814e656 to your computer and use it in GitHub Desktop.
Save ilyasotkov/20207f7b83de84b5b19d22eac814e656 to your computer and use it in GitHub Desktop.
Pytest, cycling through responses
import sys
from itertools import cycle
from unittest import mock
def test_cycle_through_responses(monkeypatch):
return_data = ["h", "hello"]
test_mock = mock.Mock(side_effect=cycle(return_data))
m = sys.modules[__name__]
monkeypatch.setattr(m, "myfunc", test_mock)
assert myfunc() == "h"
assert myfunc() == "hello"
assert myfunc() == "h"
assert myfunc() == "hello"
def myfunc():
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment