DEV Community

Discussion on: Pytest Features, That You Need in Your (Testing) Life

Collapse
 
dmitrypolo profile image
dmitrypolo

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'
Collapse
 
colinb profile image
Colin Bounouar

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 :-)

Collapse
 
martinheinz profile image
Martin Heinz

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.