0
0
PyTesttesting~20 mins

unittest.mock.patch in PyTest - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Patch Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
AAttributeError
B404
CNone
D200
Attempts:
2 left
💡 Hint
The patch replaces the requests.get call with a mock object whose status_code attribute is set to 200.
assertion
intermediate
2: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?
Amock_open.assert_called_once_with('file.txt')
Bmock_open.assert_called_with('file.txt', 'r')
Cmock_open.assert_called_once('file.txt')
Dmock_open.assert_called('file.txt')
Attempts:
2 left
💡 Hint
Use the method that checks the call count and arguments exactly once.
🔧 Debug
advanced
2: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()
ARuntimeError
BModuleNotFoundError
CAttributeError
DNo error, test passes
Attempts:
2 left
💡 Hint
The patch target string must include the full import path.
🧠 Conceptual
advanced
2:00remaining
Effect of patching with autospec=True
What is the main effect of using autospec=True in unittest.mock.patch?
AThe mock will automatically reset after each test
BThe mock will only allow attributes and methods that exist on the original object
CThe mock will record all calls without any restrictions
DThe mock will patch all instances of the object globally
Attempts:
2 left
💡 Hint
Think about how autospec helps catch incorrect method calls.
framework
expert
3: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:
A
@patch('os.remove')
def test_delete_file(mock_remove):
    delete_file('temp.txt')
    mock_remove.assert_called_once_with('temp.txt')
B
@patch('os.remove')
def test_delete_file():
    delete_file('temp.txt')
    os.remove.assert_called_once_with('temp.txt')
C
def test_delete_file():
    with patch('os.remove') as mock_remove:
        delete_file('temp.txt')
    mock_remove.assert_called_once_with('temp.txt')
D
@patch('os.remove')
def test_delete_file(mock_remove):
    delete_file('temp.txt')
    mock_remove.assert_called_once()
Attempts:
2 left
💡 Hint
Remember the patched function is passed as an argument to the test when using the decorator.