Challenge - 5 Problems
Patch Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of patching a function with unittest.mock.patch
What is the output of this pytest test when the
requests.get method is patched to return a mock with status_code=200?PyTest
import requests from unittest.mock import patch def fetch_status(): response = requests.get('http://example.com') return response.status_code def test_fetch_status(): with patch('requests.get') as mock_get: mock_get.return_value.status_code = 200 result = fetch_status() assert result == 200 print(result)
Attempts:
2 left
💡 Hint
The patch replaces the
requests.get call with a mock object whose status_code attribute is set to 200.✗ Incorrect
The patch replaces
requests.get with a mock object. Setting mock_get.return_value.status_code = 200 means the mocked response has status_code 200. So, fetch_status() returns 200.❓ assertion
intermediate2:00remaining
Correct assertion to verify a patched method call
Which assertion correctly verifies that the patched
open method was called exactly once with the argument 'file.txt'?PyTest
from unittest.mock import patch def test_file_open(): with patch('builtins.open') as mock_open: open('file.txt') # Which assertion is correct here?
Attempts:
2 left
💡 Hint
Use the method that checks the call count and arguments exactly once.
✗ Incorrect
The method
assert_called_once_with checks that the mock was called exactly once with the specified arguments. Other options either do not check call count or use incorrect method names.🔧 Debug
advanced2:00remaining
Identify the error in patching a method
What error will occur when running this test code that patches
module.Class.method incorrectly?PyTest
from unittest.mock import patch import module def test_method(): with patch('Class.method') as mock_method: module.Class().method() mock_method.assert_called_once()
Attempts:
2 left
💡 Hint
The patch target string must include the full import path.
✗ Incorrect
The patch target 'Class.method' is incomplete and does not specify the module. This causes an AttributeError because patch cannot find 'Class' at the top level.
🧠 Conceptual
advanced2:00remaining
Effect of patching with autospec=True
What is the main effect of using
autospec=True in unittest.mock.patch?Attempts:
2 left
💡 Hint
Think about how autospec helps catch incorrect method calls.
✗ Incorrect
Using
autospec=True makes the mock imitate the original object’s interface, preventing calls to methods or attributes that do not exist, helping catch errors early.❓ framework
expert3:00remaining
Correct use of patch as a decorator in pytest
Which pytest test function correctly patches
os.remove using unittest.mock.patch as a decorator and verifies it was called once?PyTest
import os from unittest.mock import patch def delete_file(filename): os.remove(filename) # Choose the correct test function:
Attempts:
2 left
💡 Hint
Remember the patched function is passed as an argument to the test when using the decorator.
✗ Incorrect
Option A correctly uses the patch decorator and receives the mock as an argument. It asserts the mock was called once with the correct argument. Option A misses the mock argument. Option A uses context manager, not decorator. Option A asserts call count but not arguments.