Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pytest monkeypatch: it is possible to return different values each time when patched method called?

In unittest I can assert to side_effect iterable with values - each of them one-by-one will be returned when patched method called, moreover I found that in unittest my patched method can return different results according to input arguments. Can I make something like that in pytest? Documentation does not mention this.

like image 796
Vitaly Zdanevich Avatar asked Dec 28 '25 00:12

Vitaly Zdanevich


2 Answers

You can use Mock's side_effect parameter to set what is to be returned for each call.

side_effect: A function to be called whenever the Mock is called. See the side_effect attribute. Useful for raising exceptions or dynamically changing return values. The function is called with the same arguments as the mock, and unless it returns DEFAULT, the return value of this function is used as the return value. Alternatively side_effect can be an exception class or instance. In this case the exception will be raised when the mock is called. If side_effect is an iterable then each call to the mock will return the next value from the iterable. A side_effect can be cleared by setting it to None.

For instance:

return_data = [3, 5]
test_mock = Mock(side_effect=return_data)
monkeypatch.setattr(your_module, "some_func", test_mock)

This way first time your call calls "some_func" it will return 3 and when you call it a second time it will return 5

Then you can if it was called twice

assert test_mock.call_count == len(return_data)

return data should be Iterable (list, set, tuple) and can contain anything you would like (int, string, object, tuples, lists, ...)

like image 187
Vlad Bezden Avatar answered Dec 30 '25 16:12

Vlad Bezden


You could certainly monkeypatch something with a class with a __call__ attribute which does whatever you want - however, nothing keeps you from using unittest.mock with pytest - there's even the pytest-mock plugin to make this easier.

like image 44
The Compiler Avatar answered Dec 30 '25 14:12

The Compiler



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!