I'm curious as to why you switch between pytest and unittest in this when the title specifies it is for pytest. Specifically when it comes to mocking why did you not illustrate the monkeypatch fixture available in pytest.
def test_my_function(monkeypatch):
with monkeypatch.context() as mc:
mc.setattr(module, 'some_func', lambda x: 'foobar')
res = my_function() # function that calls `some_function`
assert res == 'foobar'
I personally prefer to use unittest mock, that's why I showed it here, but you are right, that considering the title, maybe I should have used monkeypatch. Thanks for showing example here.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I'm curious as to why you switch between
pytest
andunittest
in this when the title specifies it is forpytest
. Specifically when it comes to mocking why did you not illustrate themonkeypatch
fixture available inpytest
.Wondering the same, also why mocking requests.get yourself when pytest-responses provide the responses fixture.
There is a lot of pytest fixtures out there, it's always nice to spread knowledge anyway :-)
I personally prefer to use
unittest
mock, that's why I showed it here, but you are right, that considering the title, maybe I should have usedmonkeypatch
. Thanks for showing example here.