A. The return_value should be a string, not an integer
B. assert statement syntax is wrong
C. mocker.patch should be called after the function call
D. The patch target string 'api.call' is incorrect or incomplete
Solution
Step 1: Check patch target correctness
The patch target 'api.call' must be the full import path. If 'api' is not imported or incomplete, patch fails.
Step 2: Validate other parts
Return value as integer is valid, patch before call is correct, and assert syntax is fine.
Final Answer:
The patch target string 'api.call' is incorrect or incomplete -> Option D
Quick Check:
Patch target must be full import path [OK]
Hint: Always use full import path in patch target string [OK]
Common Mistakes:
Using partial or wrong patch target strings
Thinking return_value must be string
Calling patch after function call
5. You want to test a function process_data() that calls fetch_from_api(). Using pytest-mock, how do you mock fetch_from_api to return different values on consecutive calls: first 'data1', then 'data2'?
hard
A. mocker.patch('module.fetch_from_api', return_value=['data1', 'data2'])
B. mocker.patch('module.fetch_from_api', side_effect=['data1', 'data2'])
C. mocker.patch('module.fetch_from_api', side_effect='data1', 'data2')
D. mocker.patch('module.fetch_from_api', return_values=['data1', 'data2'])
Solution
Step 1: Understand side_effect usage
To return different values on consecutive calls, use side_effect with a list of return values.
Step 2: Identify correct syntax
mocker.patch('module.fetch_from_api', side_effect=['data1', 'data2']) correctly uses side_effect=['data1', 'data2']. Options B, C, and D use incorrect parameters or syntax.
Final Answer:
mocker.patch('module.fetch_from_api', side_effect=['data1', 'data2']) -> Option B
Quick Check:
Use side_effect list for multiple return values [OK]
Hint: Use side_effect list to mock multiple return values [OK]
Common Mistakes:
Using return_value with a list instead of side_effect
Incorrect side_effect syntax with multiple arguments
Using non-existent return_values parameter
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.