Python Mocking with unitest.mock 2: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(45 intermediate revisions by the same user not shown)
Line 1: Line 1:
=External=
* https://docs.python.org/3/library/unittest.mock.html#module-unittest.mock
=Internal=
=Internal=


* [[Python_Mocking_with_unitest.mock#Iteration_2|Python Mocking with unitest.mock]]
* [[Python_Mocking_with_unitest.mock#Iteration_2|Python Mocking with unitest.mock]]


=Mocking a Method=
=Mocking a Class Instance with a <tt>Mock</tt> Instance=
==Using Bare <tt>Mock</tt>==
==Using <tt>Mock(SomeClass)</tt>==
==Mocking a Class Instance with a <tt>MagicMock</tt>==
{{External|https://docs.python.org/3/library/unittest.mock.html#magicmock-and-magic-method-support}}
<code>MagicMock</code> is a subclass of <code>Mock</code> with default implementations of most of the [[Python_Language_OOP#Magic_Methods|magic methods]]. You can use <code>MagicMock</code> without having to configure the magic methods yourself.
 
=Mocking a Property=
==Mocking a Property of an Actual Class Instance==
{{External|https://docs.python.org/3/library/unittest.mock.html#unittest.mock.PropertyMock}}
Assuming we want to test an actual class instance, created as part of normal logic, and modify the behavior of one of its properties for testing, the approach is to wrap the actual instance in a <code>Mock()</code> and swap the property as such:
 
<syntaxhighlight lang='py'>
class SomeClass:
    def __init__(self):
        self._color = 'blue'
 
    @property
    def color(self):
      return self._color
</syntaxhighlight>
 
<syntaxhighlight lang='py'>
sc = Mock(SomeClass)
type(sc).color = PropertyMock(return_value='red')
assert sc.color == 'red'
</syntaxhighlight>
==Mocking a Property with <tt>patch()</tt>==
<font color=darkkhaki>TO PROCESS:</font>
 
<syntaxhighlight lang='py'>
from unittest.mock import patch, PropertyMock
 
class A:
    @property
    def property_a(self):
        return "pa"


with patch('__main__.A.property_a', new_callable=PropertyMock) as mock_property:
    mock_property.return_value = 'mocked pa'
    a = A()
    assert a.property_a == 'mocked pa'
</syntaxhighlight>


The general approach is to replace at runtime the real method instance associated with the class instance to be tested with a <code>Mock</code> instance, configured to simulate various behaviors of the real method.
=Mocking a Method=
==<span id='Mocking_a_Method_with_a_Mock_Instance'></span>Mocking a Method of an Actual Class Instance with a <tt>Mock</tt> Instance==
Assuming we want to test an actual class instance, created as part of normal logic, the general approach is to replace at runtime the real method instance associated with the class instance to be tested with a <code>Mock</code> instance, configured to simulate various behaviors of the real method.


Assuming that our dependency to test with is <code>SomeClass</code>, and this class has a <code>some_method(nuance: str)</code> whose behavior we want to mock during testing, the initial implementation of the class and method could be:
Assuming that our dependency to test with is <code>SomeClass</code>, and this class has a <code>some_method(nuance: str)</code> whose behavior we want to mock during testing, the initial implementation of the class and method could be:
Line 37: Line 83:
* We can replace the behavior of the method with [[#Plug-in_Arbitrary_Behavior_with_Access_to_Invocation_Arguments|arbitrary logic]], that takes into account the arguments the method is called with.
* We can replace the behavior of the method with [[#Plug-in_Arbitrary_Behavior_with_Access_to_Invocation_Arguments|arbitrary logic]], that takes into account the arguments the method is called with.


==Simulating a Particular Return Value Irrespective of the Arguments it was Called With==
===Simulating a Particular Return Value Irrespective of the Arguments it was Called With===


Configure <code>Mock()</code> using the <code>return_value</code> argument of its constructor so no matter how the mocked method is invoked, it will always return a constant value:
Configure <code>Mock()</code> using the <code>return_value</code> argument of its constructor so no matter how the mocked method is invoked, it will always return a constant value:
Line 51: Line 97:
This is a simplistic approach, appropriate when we don't need flexible behavior depending on the arguments. For a more nuanced approach, see [[#Plug-in_Arbitrary_Behavior_with_Access_to_Invocation_Arguments|Arbitrary Behavior with Access to Invocation Arguments]] below.
This is a simplistic approach, appropriate when we don't need flexible behavior depending on the arguments. For a more nuanced approach, see [[#Plug-in_Arbitrary_Behavior_with_Access_to_Invocation_Arguments|Arbitrary Behavior with Access to Invocation Arguments]] below.


==Simulating Throwing an Exception Irrespective of the Arguments it was Called With==
===Simulating Throwing an Exception Irrespective of the Arguments it was Called With===
Configure <code>Mock()</code> using the <code>side_effect</code> argument of its constructor, by providing an exception **instance**.  Once configured as such, the mocked method will always throw exception, no matter how it is invoked.
Configure <code>Mock()</code> using the <code>side_effect</code> argument of its constructor, by providing an exception **instance**.  Once configured as such, the mocked method will always throw exception, no matter how it is invoked.


Line 62: Line 108:
except ValueError as e:
except ValueError as e:
     assert str(e) == 'test'
     assert str(e) == 'test'
</syntaxhighlight>
An alterative syntax:
<syntaxhighlight lang='py'>
c.some_method = Mock()
c.some_method.side_effect = ValueError('test')
</syntaxhighlight>
</syntaxhighlight>


This is a simplistic approach, appropriate when we don't need flexible behavior depending on the arguments. For a more nuanced approach, see [[#Plug-in_Arbitrary_Behavior_with_Access_to_Invocation_Arguments|Arbitrary Behavior with Access to Invocation Arguments]] below.
This is a simplistic approach, appropriate when we don't need flexible behavior depending on the arguments. For a more nuanced approach, see [[#Plug-in_Arbitrary_Behavior_with_Access_to_Invocation_Arguments|Arbitrary Behavior with Access to Invocation Arguments]] below.


==Plug-in Arbitrary Behavior with Access to Invocation Arguments==
===Plug-in Arbitrary Behavior with Access to Invocation Arguments===


The ultimate flexibility can be achieved configuring the <code>Mock</code> instance with its <code>wrap</code> constructor argument. <code>wrap</code> specifies an object the invocation is forwarded to, with the exact same arguments the calling layer invokes the function being mocked. The invocation can be forwarded to a [[#Function|function]] or to a [[#Class|class]].
The ultimate flexibility can be achieved configuring the <code>Mock</code> instance with its <code>wrap</code> constructor argument. <code>wrap</code> specifies an object the invocation is forwarded to, with the exact same arguments the calling layer invokes the function being mocked. The invocation can be forwarded to a [[#Function|function]] or to a [[#Class|class]].


===<span id='Function'></span>Forwarding the Invocation to a Function===
====<span id='Function'></span>Forwarding the Invocation to a Function====


Declare a delegate function that will field the invocation sent into the mocked method. A good naming convention is to postfix the name of the method with "_mock":
Declare a delegate function that will field the invocation sent into the mocked method. A good naming convention is to postfix the name of the method with "_mock":
Line 86: Line 138:
</syntaxhighlight>
</syntaxhighlight>


When we invoke into <code>some_method()</code>, the arguments will be passed unchanged to the delegate function:
When the calling layer  invokes into <code>some_method()</code>, the arguments will be passed unchanged to the delegate function, and the calling layer will get the return value of the delegate function.
<syntaxhighlight lang='py'>
<syntaxhighlight lang='py'>
assert c.some_method('SHARP') == 'sharp'
assert c.some_method('SHARP') == 'sharp'
assert c.some_method(nuance='FAINT') == 'faint'
assert c.some_method(nuance='FAINT') == 'faint'
</syntaxhighlight>
</syntaxhighlight>


The variables present in the scope of the delegate function will be visible from the function while invoked, giving us a way to "configure" the behavior:
The variables present in the scope of the delegate function will be visible from the function while invoked, giving us a way to "configure" the behavior:
Line 107: Line 158:
</syntaxhighlight>
</syntaxhighlight>


===<span id='Class'></span>Forwarding the Invocation to a Class===
====<span id='Class'></span>Forwarding the Invocation to a Class====
We can use the name of a delegate class instead the name of delegate function as argument for <code>wraps</code>. In this case, the constructor of the given class will be invoked while being passed the invocation arguments, and the calling layer will get the class insurance such constructed.
<syntaxhighlight lang='py'>
class DelegateClass:
    def __init__(self, nuance):
        self._nuance = nuance
 
 
c = SomeClass('blue')
c.some_method = Mock(wraps=DelegateClass)
 
result = c.some_method('light')
 
assert isinstance(result, DelegateClass)
assert result._nuance == 'light'
</syntaxhighlight>
 
==Mocking a Method with <tt>patch()</tt>==
<font color=darkkhaki>Deplete https://kb.novaordis.com/index.php/Python_Mocking_with_unitest.mock#Mocking_a_Method</font>
=Mock Introspection=
Once the mock was used and was invoked into, it is important to be able to tell whether it was used in the proper way. This is where observability comes in.
 
<font color=darkkhaki>Deplete: {{Internal|Python_Mocking_with_unitest.mock#Asserting_Invocations_on_Mock| Asserting_Invocations_on_Mock }}</font>
 
==Mock was Used as a Target Instance==
 
Useful accessors: <code>method_calls</code>, <code>mock_calls</code>. If the mock fielded invocations, these accessors return lists with <code>[[#Call|_Call]]</code> instances.
 
<font color=darkkhaki><code>method_calls</code> was seen empty at times, use <code>mock_calls</code>.</font>
 
<font color=darkkhaki>Also, when a new method invocation was sent into the Mock instance, the Mock instance "gains" a new attribute with the name of the invoked method.</font>
 
===Property Interaction on the Target Instance===
 
Note that a property read or write does not count as method call, or mock call.
 
If a property is "written" on a mock, it can be tested naturally as follows:
 
<syntaxhighlight lang='py'>
target = Mock()
target.color = 'blue'
assert target.color == 'blue'
</syntaxhighlight>
 
To verify that a property was not invoked, get its value and verify it's a <code>Mock</code> instance (if the property was set, the value would be a non-mock).
<syntaxhighlight lang='py'>
target = Mock()
# ensure a property was not invoked
assert isinstance(target.color, Mock)
</syntaxhighlight>
 
==Mock was Used as a Mock Method==
 
===Accessors===
====<tt>called</tt>====
====<tt>call_count</tt>====
Return the number of times the mock method was called into, as an integer.
<syntaxhighlight lang='py'>
assert some_instance.some_mocked_method.call_count == 2
</syntaxhighlight>
 
====<tt>call_args_list</tt>====
<code>call_args_list</code> is a list of <code>_Call</code> instances, corresponding to the calls (invocations) that were made on the mock method, in order in which they are invoked. The length of the list is equal with <code>[[#call_count|mock_method.call_count]]</code>
<syntaxhighlight lang='py'>
assert mock_instance.mock_method.call_count == 2
first_invocation = mock_instance.mock_method.call_args_list[0]
second_invocation = mock_instance.mock_method.call_args_list[1]
assert first_invocation.args == ('test-workspace-dir', 'test-repo-name', 'test/path', 'test.config', 'this is configuration')
assert second_invocation.args == ('test-workspace-dir', 'test-repo-name', 'test/path', 'test.config', 'this is configuration')
</syntaxhighlight>
 
<font color=darkkhaki>
 
Call arguments for each call can be obtained with the index operator []:
 
<syntaxhighlight lang='py'>
args_for_first_call = mock.call_args_list[0]
args_for_second_call = mock.call_args_list[1]
...
</syntaxhighlight>
 
Positional arguments are maintained in a tuple and can be obtained with the <code>args</code> property. It returns a tuple, which may be empty:
<syntaxhighlight lang='py'>
mock.call_args_list[0].args
</syntaxhighlight>
 
Named arguments are maintained in a dictionary, which can be obtained with the <code>kwargs</code> property. It returns a dictionary, which may be empty:
<syntaxhighlight lang='py'>
mock.call_args_list[0].kwargs
mock.call_args_list[0].kwargs['some_arg']
</syntaxhighlight>
</font>
====<tt>call_args</tt>====
 
===Methods===
====<tt>assert_called_once_with()</tt>====
 
<syntaxhighlight lang='py'>
mock.assert_called_once_with("some concrete arg 1", unittest.mock.ANY)
</syntaxhighlight>
 
<font color=darkkhaki>TO further document: https://docs.python.org/3/library/unittest.mock.html#unittest.mock.ANY</font>
 
=<span id='Call'></span>A <tt>_Call</tt> Instance=
 
A <code>_Call</code> instance models a method invocation into a <code>Mock</code> instance. When a <code>Mock</code>instance was used to simulate an instance being invoked into, the invocations can be introspected by accessing <code>mock_calls</code> or <code>method_calls</code>, which collect invocations as  <code>_Call</code> instances.
 
Assuming that we have access to a <code>_Call</code> instance, then the name of the method that was called on the mock can be obtained with <code>c[0]</code>, the positional arguments can be obtained as a tuple with <code>c[1]</code> or <code>c.args</code>, and named arguments can be obtained as a dict with <code>c[2]</code> or <code>c.kwargs</code>.
 
<syntaxhighlight lang='py'>
target = Mock()
 
target.some_method('blue', 5)
target.some_method(color='blue', size=5)
 
assert len(target.method_calls) == 2                  # the "calls" are collected in order, in mock.method_calls
 
first_recorded_call = target.method_calls[0]
 
assert first_recorded_call[0] == 'some_method'        # the method name
assert len(first_recorded_call[1]) == 2                # a tuple containing positional arguments
assert first_recorded_call[1][0] == 'blue'            # first positional argument
assert first_recorded_call[1][1] == 5                  # second positional argument
assert len(first_recorded_call.args) == 2              # alternative way of obtaining positional arguments
assert first_recorded_call.args[0] == 'blue'          # first positional argument
assert first_recorded_call.args[1] == 5                # second positional argument
assert len(first_recorded_call[2]) == 0                # a dict containing named arguments
assert len(first_recorded_call.kwargs) == 0            # alternative way of obtaining named arguments
 
second_recorded_call = target.method_calls[1]
 
assert second_recorded_call[0] == 'some_method'        # the method name
assert len(second_recorded_call[1]) == 0              # a tuple containing positional arguments
assert len(second_recorded_call.args) == 0            # alternative way of obtaining positional arguments
assert len(second_recorded_call[2]) == 2              # a dict containing named arguments
assert second_recorded_call[2]['color'] == 'blue'      # the 'color' named argument
assert second_recorded_call[2]['size'] == 5            # the 'size' named argument
assert len(second_recorded_call.kwargs) == 2          # alternative way of obtaining named arguments
assert second_recorded_call.kwargs['color'] == 'blue'  # the 'color' named argument
assert second_recorded_call.kwargs['size'] == 5        # the 'size' named argument
</syntaxhighlight>

Latest revision as of 18:59, 18 August 2023

External

Internal

Mocking a Class Instance with a Mock Instance

Using Bare Mock

Using Mock(SomeClass)

Mocking a Class Instance with a MagicMock

https://docs.python.org/3/library/unittest.mock.html#magicmock-and-magic-method-support

MagicMock is a subclass of Mock with default implementations of most of the magic methods. You can use MagicMock without having to configure the magic methods yourself.

Mocking a Property

Mocking a Property of an Actual Class Instance

https://docs.python.org/3/library/unittest.mock.html#unittest.mock.PropertyMock

Assuming we want to test an actual class instance, created as part of normal logic, and modify the behavior of one of its properties for testing, the approach is to wrap the actual instance in a Mock() and swap the property as such:

 
class SomeClass:
    def __init__(self):
        self._color = 'blue'

    @property
    def color(self):
       return self._color
 
sc = Mock(SomeClass)
type(sc).color = PropertyMock(return_value='red')
assert sc.color == 'red'

Mocking a Property with patch()

TO PROCESS:

from unittest.mock import patch, PropertyMock

class A:
    @property
    def property_a(self):
        return "pa"

with patch('__main__.A.property_a', new_callable=PropertyMock) as mock_property:
    mock_property.return_value = 'mocked pa'
    a = A()
    assert a.property_a == 'mocked pa'

Mocking a Method

Mocking a Method of an Actual Class Instance with a Mock Instance

Assuming we want to test an actual class instance, created as part of normal logic, the general approach is to replace at runtime the real method instance associated with the class instance to be tested with a Mock instance, configured to simulate various behaviors of the real method.

Assuming that our dependency to test with is SomeClass, and this class has a some_method(nuance: str) whose behavior we want to mock during testing, the initial implementation of the class and method could be:

 
class SomeClass:
    def __init__(self, color: str):
        self._color = color

    def some_method(self, nuance: str) -> str:
        return f'{nuance} {self._color}'.upper()

The normal behavior of the method some_method(nuance: str) is reflected by:

 
c = SomeClass('blue')
assert c.some_method('dark') == 'DARK BLUE'

c._color = 'red'
assert c.some_method('light') == 'LIGHT RED'

We can mock the behavior of the some_method(nuance: str) method in the following ways:

  • We can return a constant value, regardless of the arguments the method is invoked with.
  • We can raise an exception, regardless of the arguments the method is invoked with.
  • We can replace the behavior of the method with arbitrary logic, that takes into account the arguments the method is called with.

Simulating a Particular Return Value Irrespective of the Arguments it was Called With

Configure Mock() using the return_value argument of its constructor so no matter how the mocked method is invoked, it will always return a constant value:

c = SomeClass('blue')
c.some_method = Mock(return_value='something completely arbitrary')
assert c.some_method('argument does not matter') == 'something completely arbitrary'

Irrespective of how the method is invoked in testing, the calling code will always get the value configured with return_value on the mock.

This is a simplistic approach, appropriate when we don't need flexible behavior depending on the arguments. For a more nuanced approach, see Arbitrary Behavior with Access to Invocation Arguments below.

Simulating Throwing an Exception Irrespective of the Arguments it was Called With

Configure Mock() using the side_effect argument of its constructor, by providing an exception **instance**. Once configured as such, the mocked method will always throw exception, no matter how it is invoked.

c = SomeClass('blue')
c.some_method = Mock(side_effect=ValueError('test'))

try:
    c.some_method('argument does not matter')
except ValueError as e:
    assert str(e) == 'test'

An alterative syntax:

c.some_method = Mock()
c.some_method.side_effect = ValueError('test')

This is a simplistic approach, appropriate when we don't need flexible behavior depending on the arguments. For a more nuanced approach, see Arbitrary Behavior with Access to Invocation Arguments below.

Plug-in Arbitrary Behavior with Access to Invocation Arguments

The ultimate flexibility can be achieved configuring the Mock instance with its wrap constructor argument. wrap specifies an object the invocation is forwarded to, with the exact same arguments the calling layer invokes the function being mocked. The invocation can be forwarded to a function or to a class.

Forwarding the Invocation to a Function

Declare a delegate function that will field the invocation sent into the mocked method. A good naming convention is to postfix the name of the method with "_mock":

def some_method_mock(nuance):
   return nuance.lower()

Then configure the Mock() instance that will replace the mocked method using the wraps constructor argument, providing the name of the newly declared function. Careful to provide the name of the function, not to invoke the function in place:

c = SomeClass('blue')
c.some_method = Mock(wraps=some_method_mock) # NOT Mock(wraps=some_method_mock())

When the calling layer invokes into some_method(), the arguments will be passed unchanged to the delegate function, and the calling layer will get the return value of the delegate function.

assert c.some_method('SHARP') == 'sharp'
assert c.some_method(nuance='FAINT') == 'faint'

The variables present in the scope of the delegate function will be visible from the function while invoked, giving us a way to "configure" the behavior:

MOCK_CONFIGURATION = '<>'

def some_method_mock(nuance):
    return MOCK_CONFIGURATION + ' ' + nuance.lower()

c = SomeClass('blue')
c.some_method = Mock(wraps=some_method_mock)

assert c.some_method('SHARP') == '<> sharp'

Forwarding the Invocation to a Class

We can use the name of a delegate class instead the name of delegate function as argument for wraps. In this case, the constructor of the given class will be invoked while being passed the invocation arguments, and the calling layer will get the class insurance such constructed.

class DelegateClass:
    def __init__(self, nuance):
        self._nuance = nuance


c = SomeClass('blue')
c.some_method = Mock(wraps=DelegateClass)

result = c.some_method('light')

assert isinstance(result, DelegateClass)
assert result._nuance == 'light'

Mocking a Method with patch()

Deplete https://kb.novaordis.com/index.php/Python_Mocking_with_unitest.mock#Mocking_a_Method

Mock Introspection

Once the mock was used and was invoked into, it is important to be able to tell whether it was used in the proper way. This is where observability comes in.

Deplete:

Asserting_Invocations_on_Mock

Mock was Used as a Target Instance

Useful accessors: method_calls, mock_calls. If the mock fielded invocations, these accessors return lists with _Call instances.

method_calls was seen empty at times, use mock_calls.

Also, when a new method invocation was sent into the Mock instance, the Mock instance "gains" a new attribute with the name of the invoked method.

Property Interaction on the Target Instance

Note that a property read or write does not count as method call, or mock call.

If a property is "written" on a mock, it can be tested naturally as follows:

target = Mock()
target.color = 'blue'
assert target.color == 'blue'

To verify that a property was not invoked, get its value and verify it's a Mock instance (if the property was set, the value would be a non-mock).

target = Mock()
# ensure a property was not invoked
assert isinstance(target.color, Mock)

Mock was Used as a Mock Method

Accessors

called

call_count

Return the number of times the mock method was called into, as an integer.

assert some_instance.some_mocked_method.call_count == 2

call_args_list

call_args_list is a list of _Call instances, corresponding to the calls (invocations) that were made on the mock method, in order in which they are invoked. The length of the list is equal with mock_method.call_count

assert mock_instance.mock_method.call_count == 2
first_invocation = mock_instance.mock_method.call_args_list[0]
second_invocation = mock_instance.mock_method.call_args_list[1]
assert first_invocation.args == ('test-workspace-dir', 'test-repo-name', 'test/path', 'test.config', 'this is configuration')
assert second_invocation.args == ('test-workspace-dir', 'test-repo-name', 'test/path', 'test.config', 'this is configuration')

Call arguments for each call can be obtained with the index operator []:

args_for_first_call = mock.call_args_list[0]
args_for_second_call = mock.call_args_list[1]
...

Positional arguments are maintained in a tuple and can be obtained with the args property. It returns a tuple, which may be empty:

mock.call_args_list[0].args

Named arguments are maintained in a dictionary, which can be obtained with the kwargs property. It returns a dictionary, which may be empty:

mock.call_args_list[0].kwargs
mock.call_args_list[0].kwargs['some_arg']

call_args

Methods

assert_called_once_with()

mock.assert_called_once_with("some concrete arg 1", unittest.mock.ANY)

TO further document: https://docs.python.org/3/library/unittest.mock.html#unittest.mock.ANY

A _Call Instance

A _Call instance models a method invocation into a Mock instance. When a Mockinstance was used to simulate an instance being invoked into, the invocations can be introspected by accessing mock_calls or method_calls, which collect invocations as _Call instances.

Assuming that we have access to a _Call instance, then the name of the method that was called on the mock can be obtained with c[0], the positional arguments can be obtained as a tuple with c[1] or c.args, and named arguments can be obtained as a dict with c[2] or c.kwargs.

target = Mock()

target.some_method('blue', 5)
target.some_method(color='blue', size=5)

assert len(target.method_calls) == 2                  # the "calls" are collected in order, in mock.method_calls

first_recorded_call = target.method_calls[0]

assert first_recorded_call[0] == 'some_method'         # the method name
assert len(first_recorded_call[1]) == 2                # a tuple containing positional arguments
assert first_recorded_call[1][0] == 'blue'             # first positional argument
assert first_recorded_call[1][1] == 5                  # second positional argument
assert len(first_recorded_call.args) == 2              # alternative way of obtaining positional arguments
assert first_recorded_call.args[0] == 'blue'           # first positional argument
assert first_recorded_call.args[1] == 5                # second positional argument
assert len(first_recorded_call[2]) == 0                # a dict containing named arguments
assert len(first_recorded_call.kwargs) == 0            # alternative way of obtaining named arguments

second_recorded_call = target.method_calls[1]

assert second_recorded_call[0] == 'some_method'        # the method name
assert len(second_recorded_call[1]) == 0               # a tuple containing positional arguments
assert len(second_recorded_call.args) == 0             # alternative way of obtaining positional arguments
assert len(second_recorded_call[2]) == 2               # a dict containing named arguments
assert second_recorded_call[2]['color'] == 'blue'      # the 'color' named argument
assert second_recorded_call[2]['size'] == 5            # the 'size' named argument
assert len(second_recorded_call.kwargs) == 2           # alternative way of obtaining named arguments
assert second_recorded_call.kwargs['color'] == 'blue'  # the 'color' named argument
assert second_recorded_call.kwargs['size'] == 5        # the 'size' named argument