unittest.mock.patch helps you replace parts of your code during tests. This lets you control what those parts do, so you can test your code easily.
unittest.mock.patch in PyTest
from unittest.mock import patch @patch('module_name.function_name') def test_function(mock_function): mock_function.return_value = expected_value # your test code here
Use the full path to the function or object you want to replace, like 'package.module.function'.
The patched object is replaced only during the test and restored after it finishes.
from unittest.mock import patch @patch('math.sqrt') def test_sqrt(mock_sqrt): mock_sqrt.return_value = 3 result = mock_sqrt(9) assert result == 3
from unittest.mock import patch import requests def test_api_call(): with patch('requests.get') as mock_get: mock_get.return_value.status_code = 200 response = requests.get('http://example.com') assert response.status_code == 200
This test replaces requests.get with a mock that returns a response with status_code 404. The test checks if fetch_status returns 404 as expected.
from unittest.mock import patch import requests def fetch_status(url): response = requests.get(url) return response.status_code @patch('requests.get') def test_fetch_status(mock_get): mock_get.return_value.status_code = 404 status = fetch_status('http://fakeurl.com') assert status == 404 print('Test passed') if __name__ == '__main__': test_fetch_status()
Always patch where the function or object is used, not where it is defined.
Remember to set return_value or side_effect on the mock to control its behavior.
Using patch as a decorator or context manager depends on how long you want the mock to last.
unittest.mock.patch lets you replace parts of your code during tests to control behavior.
Use patch to avoid real calls and test your code in isolation.
Patch can be used as a decorator or context manager for flexible mocking.
Practice
unittest.mock.patch in pytest?Solution
Step 1: Understand the role of patch
unittest.mock.patchis used to replace parts of your code temporarily during tests, so you can control how those parts behave without running the real code.Step 2: Compare with other options
The other options describe generating test data, speeding up tests by skipping them, or checking syntax, none of whichpatchperforms.Final Answer:
To replace parts of the code temporarily during tests to control behavior -> Option DQuick Check:
Patch replaces code parts temporarily [OK]
- Thinking patch speeds up tests by skipping them
- Confusing patch with test data generation
- Believing patch checks code syntax
requests.get using a decorator in pytest?Solution
Step 1: Understand patch decorator syntax
The patch decorator requires the target as a string with the full path, like 'module.function'. So, '@patch('requests.get')' is correct.Step 2: Identify incorrect syntax
@patch(requests.get) misses quotes around the target, @patch('requests.get()') adds incorrect parentheses, and @patch('requests->get') uses an invalid arrow symbol.Final Answer:
@patch('requests.get') -> Option CQuick Check:
Patch target must be a string path [OK]
- Omitting quotes around the target string
- Adding parentheses after the function name
- Using invalid symbols like arrows in the target
import requests
from unittest.mock import patch
def fetch_data():
return requests.get('http://example.com').status_code
@patch('requests.get')
def test_fetch(mock_get):
mock_get.return_value.status_code = 404
print(fetch_data())
if __name__ == '__main__':
test_fetch()Solution
Step 1: Understand patch effect on requests.get
The decorator replacesrequests.getwith a mock object. The mock'sreturn_value.status_codeis set to 404.Step 2: Trace fetch_data call
Whenfetch_data()callsrequests.get, it gets the mock, whosestatus_codeis 404, so it returns 404.Final Answer:
404 -> Option AQuick Check:
Mock return_value.status_code = 404 means fetch_data() returns 404 [OK]
- Expecting real HTTP status 200 instead of mocked 404
- Confusing mock object with None
- Thinking patch causes an error here
from unittest.mock import patch
def test_api_call():
with patch('api.requests.get') as mock_get:
mock_get.status_code = 200
response = api_call()
assert response == 'success'Solution
Step 1: Understand mock object structure
When patching a function, the mock object replaces the function. The function call returnsmock_get.return_value. So, attributes likestatus_codemust be set onmock_get.return_value.Step 2: Identify the error in code
The code setsmock_get.status_code = 200which is incorrect; it should bemock_get.return_value.status_code = 200.Final Answer:
mock_get.status_code should be set on mock_get.return_value, not mock_get -> Option BQuick Check:
Set attributes on return_value, not mock function [OK]
- Setting attributes directly on the mock function
- Forgetting quotes around patch target
- Thinking patch must be decorator only
- Miswriting assert syntax
process_data() that calls fetch_from_api() internally. You want to patch fetch_from_api only during the test to return a fixed value. Which is the best way to do this using unittest.mock.patch?Solution
Step 1: Understand patch scope and usage
Using@patch('module.fetch_from_api')decorator temporarily replacesfetch_from_apionly during the test, allowing control over its return value.Step 2: Evaluate other options
Replacefetch_from_apiglobally in the module before running tests changes global code, which is unsafe. Modifyfetch_from_apicode to return fixed value during tests modifies production code, which is bad practice. Callfetch_from_apidirectly in the test without patching does not isolate the test.Final Answer:
Use @patch('module.fetch_from_api') decorator on the test function and set mock_fetch.return_value -> Option AQuick Check:
Patch with decorator for safe, temporary replacement [OK]
- Changing global code instead of patching
- Modifying production code for tests
- Not patching and calling real function
Ready to prove it?
Pass the test and this pattern moves unknown
