@@ -14,7 +14,16 @@ def __init__(self):
1414 def __call__ (self , * args , ** kwargs ):
1515 self ._calls .append (call (* args , ** kwargs ))
1616
17+ def assert_called (self ):
18+ """Assert that the mock was called at least once."""
19+ assert len (self ._calls ) > 0 , "Expected mock to be called, but it was not."
20+
21+ def assert_not_called (self ):
22+ """Assert that the mock was not called."""
23+ assert len (self ._calls ) == 0 , "Expected mock to not be called, but it was."
24+
1725 def assert_called_with (self , * args , ** kwargs ):
26+ """Assert that the mock was last called with the given arguments."""
1827 # First call should be self, so we prepend it
1928 expected_args = [self ] + list (args )
2029 expectation = call (* expected_args , ** kwargs )
@@ -24,15 +33,31 @@ def assert_called_with(self, *args, **kwargs):
2433 expectation , self ._calls [- 1 ]
2534 )
2635
36+ def assert_has_calls (self , calls ):
37+ """Assert that the mock has the expected calls with arguments."""
38+ assert self ._calls == calls , "Expected calls {}, got {}" .format (
39+ calls , self ._calls
40+ )
41+
2742
2843class AsyncMock (Mock ):
2944 """An async version of Mock that can be awaited."""
3045
3146 async def __call__ (self , * args , ** kwargs ):
3247 return super ().__call__ (self , * args , ** kwargs )
3348
49+ def assert_awaited (self ):
50+ """Assert that the async mock was awaited at least once."""
51+ return super ().assert_called ()
52+
53+ def assert_not_awaited (self ):
54+ """Assert that the async mock was not awaited."""
55+ return super ().assert_not_called ()
56+
3457 def assert_awaited_with (self , * args , ** kwargs ):
58+ """Assert that the async mock was last awaited with the given arguments."""
3559 return super ().assert_called_with (* args , ** kwargs )
3660
3761 def assert_has_awaits (self , awaits ):
38- assert self ._calls == awaits
62+ """Assert that the async mock has the expected awaits with arguments."""
63+ return super ().assert_has_calls (awaits )
0 commit comments