test_aide.functions.assert_function_call_count

test_aide.functions.assert_function_call_count(mocker, target, attribute, expected_n_calls, **kwargs)[source]

Assert a function has been called a given number of times. This should be used as a context manager, see example below.

Parameters
  • mocker (pytest_mock.plugin.MockerFixture) – The mocker fixture from the pytest_mock package.

  • target (object) – Object with function to test is called.

  • attribute (str) – Name of the method to mock on the target object

  • expected_n_calls (int) – Expected number of calls to target.attribute.

  • **kwargs (any) – Arbitrary keyword arguments passed on to mocker.patch.object.

Examples

>>> import test_aide as ta
>>> import random
>>>
>>> def test_number_calls_to_function(mocker):
...     with ta.functions.assert_function_call_count(mocker, random, "choice", 3):
...         random.choice([1, 2, 3])
...         random.choice([4, 5, 6])
...         random.choice([7, 8, 9])