2

How can I use mock.patch on a function, so I have an access to methods .assert_called etc. and simultaneously I can still preserve original functionality of the functions?

Here is example code:

from unittest import mock

def foo(arg):
    print(arg)

def tested():
    foo('hi')

@mock.patch('__main__.foo')
def test(foo):
    tested()
    foo.assert_called_once()

test()

I want it to test if the foo function was called just once but I still need it to print hi.

1 Answer 1

1

Oh. I have it solved already. I'v just needed to add parameter side_effect to the decorator :-)

Like this:

@mock.patch('__main__.foo', side_effect=foo)
def test(foo):
    ...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.