0

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:

enter image description here

How to write test properly?

  • 1
    Yuor patch string is probably wrong, see [where to patch](https://docs.python.org/3/library/unittest.mock.html#id6). If your code is really in the same module as the test you can use something like `patch(f"{__name__}.send")`. – MrBean Bremen Dec 26 '22 at 09:05
  • @MrBeanBremen, well I tried both pitest.decorat.send and decorat.send. Ran the test by PyCharm launcher and from console but still not working – Альберт Александров Dec 26 '22 at 09:37
  • Well, did you try the patch string I mentioned? Looks like you indeed have the test together with the code, so that is what you would need. Depending on how `pytest` is called, [the module name may be different](https://stackoverflow.com/a/65379152/12480730), so that is the best way to do it. – MrBean Bremen Dec 26 '22 at 13:47
  • @MrBeanBremen, you're right. Also my bad - I shouldn't have do assert for assert_called_with. Post as an answer your comment - I will accept it – Альберт Александров Dec 26 '22 at 14:56
  • 1
    Ah right, I missed that you did that `assert`. Glad you solved it, though I won't add an answer, as this has been answered before and is most likely a duplicate. – MrBean Bremen Dec 26 '22 at 17:46

0 Answers0