I need to test if a function send
was called with specific key
:
from unittest.mock import patch, MagicMock
def send(key=None):
print('key is', key)
def do_smth():
send(key='a')
@patch('pitest.decorat.send', return_value=MagicMock())
def test_do_smth(mo):
do_smth()
assert mo.assert_called_with(key='a')
But I get error:
Launching pytest with arguments /Users/alber.aleksandrov/PycharmProjects/Playground/pitest/decorat.py --no-header --no-summary -q in /Users/alber.aleksandrov/PycharmProjects/Playground/pitest
============================= test session starts ==============================
collecting ... collected 1 item
decorat.py::test_do_smth FAILED [100%]key is a
decorat.py:11 (test_do_smth)
mo = <MagicMock name='send' id='4551497888'>
@patch('pitest.decorat.send', return_value=MagicMock())
def test_do_smth(mo):
do_smth()
> assert mo.assert_called_with(key='a')
decorat.py:15:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = <MagicMock name='send' id='4551497888'>, args = (), kwargs = {'key': 'a'}
expected = "send(key='a')", actual = 'not called.'
error_message = "expected call not found.\nExpected: send(key='a')\nActual: not called."
def assert_called_with(self, /, *args, **kwargs):
"""assert that the last call was made with the specified arguments.
Raises an AssertionError if the args and keyword args passed in are
different to the last call to the mock."""
if self.call_args is None:
expected = self._format_mock_call_signature(args, kwargs)
actual = 'not called.'
error_message = ('expected call not found.\nExpected: %s\nActual: %s'
% (expected, actual))
> raise AssertionError(error_message)
E AssertionError: expected call not found.
E Expected: send(key='a')
E Actual: not called.
Files:
How to write test properly?