Skip to content Skip to sidebar Skip to footer

Writing A Contextmanager That Patches An Object

In my Python 3 test code, I have a lot of these statements: from unittest.mock import patch user = User(...) with patch.object(MyAuthenticationClass, 'authenticate', return_value=

Solution 1:

You can write a simple wrapper like this:

defrequest_user(user):
    return patch.object(MyAuthenticationClass, 'authenticate', return_value=(user, 'token')

And use it:

with request_user(user):
    # ...

Post a Comment for "Writing A Contextmanager That Patches An Object"