0
0
PyTesttesting~10 mins

Mock call assertions in PyTest - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to assert the mock was called exactly once.

PyTest
mock_function.[1]()
Drag options to blanks, or click blank then click option'
Aassert_called
Bassert_called_once
Cassert_called_once_with
Dassert_not_called
Attempts:
3 left
💡 Hint
Common Mistakes
Using assert_called_once_with without specifying arguments
Using assert_called which only checks if called at least once
2fill in blank
medium

Complete the code to assert the mock was called with specific arguments.

PyTest
mock_function.[1](42, 'test')
Drag options to blanks, or click blank then click option'
Aassert_not_called
Bassert_called
Cassert_called_with
Dassert_called_once_with
Attempts:
3 left
💡 Hint
Common Mistakes
Using assert_called_with which allows multiple calls
Using assert_called which ignores arguments
3fill in blank
hard

Fix the error in the assertion to check the mock was never called.

PyTest
mock_function.[1]()
Drag options to blanks, or click blank then click option'
Aassert_not_called
Bassert_called
Cassert_called_once
Dassert_called_once_with
Attempts:
3 left
💡 Hint
Common Mistakes
Using assert_called which expects at least one call
Using assert_called_once which expects exactly one call
4fill in blank
hard

Fill both blanks to assert the mock was called twice and with specific arguments on the second call.

PyTest
assert mock_function.call_count == [1]
mock_function.[2](100, key='value')
Drag options to blanks, or click blank then click option'
A2
Bassert_called_once_with
Cassert_any_call
D3
Attempts:
3 left
💡 Hint
Common Mistakes
Using assert_called_once_with which only checks one call
Checking call_count equals 3 instead of 2
5fill in blank
hard

Fill all three blanks to assert the mock was called with specific arguments and check the first call's arguments.

PyTest
mock_function.[1](5, 10)
assert mock_function.call_args == [2]
assert mock_function.call_args_list[0] == [3]
Drag options to blanks, or click blank then click option'
Aassert_called_with
B(5, 10)
C((5, 10), {})
Dassert_called_once_with
Attempts:
3 left
💡 Hint
Common Mistakes
Using assert_called_with which allows multiple calls
Using (5, 10) instead of ((5, 10), {}) for the Call object