3

When using mocker from pytest-mock, how can I use has_calls to check the calls to a specific method without checking all the calls made to the return value of that method?

For example:

import subprocess
from unittest import mock

def test_foo(mocker):
    m = mocker.patch('test_main.subprocess')
    r1 = subprocess.run(['foo'])
    r1.check_returncode()
    r2 = subprocess.run(['bar'])
    r2.check_returncode()

    m.run.assert_has_calls([
        mock.call(['foo']),
        mock.call(['bar']),
    ])

Fails with:

E       AssertionError: Calls not found.
E       Expected: [call(['foo']), call(['bar'])]
E       Actual: [call(['foo']),
E        call().check_returncode(),
E        call(['bar']),
E        call().check_returncode()]

Why are calls to the return value also included? I just want to check that I had a call to subprocess.run(['foo']) followed by a call to subprocess.run(['bar']). I don't care about calls made to anything it returns, but I do care about the order of calls to run itself.

0

2 Answers 2

3

Add any_order=True to your assert_has_calls call, like this:

m.run.assert_has_calls([
    mock.call(['foo']),
    mock.call(['bar']),
], any_order=True)

The reason is, it looks like assert_has_calls uses the mock_calls attribute on a mock which based on the documentation (https://docs.python.org/3/library/unittest.mock.html#unittest.mock.Mock.mock_calls):

records all calls to the mock object, its methods, magic methods and return value mocks.

With assert_has_calls though, it's not an exact matching problem it was just the ordering that was failing.

EDIT: Since order matters, use call_args_list. Like this:

def test_foo(mocker):
    m = mocker.patch('subprocess_test.subprocess')
    r1 = subprocess.run(['foo'])
    r1.check_returncode()
    r2 = subprocess.run(['bar'])
    r2.check_returncode()

    assert [mock.call(['foo']), mock.call(['bar'])] == m.run.call_args_list
2
  • Thanks for pointing me at mock_calls, that explains the behavior. However, any_order will not work for me: > I just want to check that I had a call to subprocess.run(['foo']) followed by a call to subprocess.run(['bar'])
    – rcorre
    Sep 18, 2018 at 15:40
  • 1
    Just wanted to point out here that call_args_list is the real answer here, and should be accepted (but also probably that the answer itself should be edited to put that part of the answer prominently).
    – A. Wilson
    Feb 22, 2019 at 19:19
0

Use assert_any_call:

m.run.assert_any_call(['foo'])
m.run.assert_any_call(['bar'])
1
  • assert_any_call does not allow me to check the order. > > I just want to check that I had a call to subprocess.run(['foo']) followed by a call to subprocess.run(['bar']). Both answers seem to miss this, so I'll edit the question to make it more clear.
    – rcorre
    Sep 18, 2018 at 15:41

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.