mercredi 3 octobre 2018

Shared mock object

I want to reuse the same mock.MagicMock instance for several different methods. Is this the right way to do that:

my_mock = mock.MagicMock()
my_mock.side_effect = [1, 1.5, 5]
def get_mock():
  return my_mock

with mock.patch.object(module_a, 'time', new_callable=get_mock):
  with mock.patch.object(module_b, 'time', new_callable=get_mock):
    do_stuff()

my_mock.time.assert_called()  # or whatever else I want to check

I was thinking maybe there was a simpler way? Also am I missing any reason why it might be unsafe to have nested with statements with the same object returned from __enter__?

Aucun commentaire:

Enregistrer un commentaire