1 year ago

#316206

test-img

Remotec

Python mock not applied to object under test

I am attempting to mock an object to perform some testing

test.py

@patch("api.configuration.client.get_configuration")
def test(mock_client_get_configuration):
    mock_client_get_configuration.return_value = "123"

    result = code_under_test()

    assert result

Inside code_under_test() I make the concrete call to get_configuration().

code.py

from api.configuration.client import get_configuration

def code_under_test():
    config = get_configuration("a", "b")

However, whenever I run my test, get_configuration is always the concrete version and not my mock.

If I add a print in my test, I can see that mock_client_get_configuration is a MagicMock.

If I add a print inside code_under_test, then get_configuration is always the actual <function.... and not my mock.

I feel somewhere I am going wrong in how I create my mock. Perhaps it is because my mock does not mimic the two parameters needed on the concrete call so the signature is incorrect?

I'm using Python 3.9 and pytest 7.0.1.

python

unit-testing

mocking

pytest

magicmock

0 Answers

Your Answer

Accepted video resources