Test Overview
This test uses unittest.mock.patch to replace a function with a mock during the test. It verifies that the patched function is called with expected arguments and returns the mocked value.
This test uses unittest.mock.patch to replace a function with a mock during the test. It verifies that the patched function is called with expected arguments and returns the mocked value.
from unittest.mock import patch import pytest def get_data(): # Imagine this fetches data from a slow external source return "real data" def process_data(): data = get_data() return f"Processed {data}" @patch('__main__.get_data') def test_process_data(mock_get_data): mock_get_data.return_value = "mocked data" result = process_data() mock_get_data.assert_called_once() assert result == "Processed mocked data"
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts | Test runner initializes pytest environment | — | PASS |
| 2 | Patch '__main__.get_data' with a mock object | The get_data function is replaced by mock_get_data during test | — | PASS |
| 3 | Call process_data(), which calls the patched get_data() | process_data calls mock_get_data instead of real get_data | — | PASS |
| 4 | mock_get_data returns 'mocked data' as set by return_value | process_data receives 'mocked data' from mock_get_data | — | PASS |
| 5 | Assert mock_get_data was called exactly once | mock_get_data call count is 1 | mock_get_data.assert_called_once() | PASS |
| 6 | Assert process_data() returned 'Processed mocked data' | Result string is 'Processed mocked data' | assert result == 'Processed mocked data' | PASS |
| 7 | Test ends successfully | All assertions passed, test completes | — | PASS |
unittest.mock.patch in pytest?unittest.mock.patch is used to replace parts of your code temporarily during tests, so you can control how those parts behave without running the real code.patch performs.requests.get using a decorator in pytest?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()requests.get with a mock object. The mock's return_value.status_code is set to 404.fetch_data() calls requests.get, it gets the mock, whose status_code is 404, so it returns 404.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'mock_get.return_value. So, attributes like status_code must be set on mock_get.return_value.mock_get.status_code = 200 which is incorrect; it should be mock_get.return_value.status_code = 200.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?@patch('module.fetch_from_api') decorator temporarily replaces fetch_from_api only during the test, allowing control over its return value.fetch_from_api globally in the module before running tests changes global code, which is unsafe. Modify fetch_from_api code to return fixed value during tests modifies production code, which is bad practice. Call fetch_from_api directly in the test without patching does not isolate the test.Pass the test and this pattern moves unknown