python mock side_effect or return_value dependent on call_count

If I understand your question correctly, you do it by setting side_effect to an iterable. For your simple case:

>>> mock_poll = Mock(side_effect=[None, 'data'])
>>> mock_poll()
None
>>> mock_poll()
'data'

If you want to allow for an unlimited number of calls, use the itertools cycle and chain functions:

>>> mock_poll = Mock(side_effect=chain(['first'], cycle(['others'])))

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)