How to Mock Return Value in pytest: Simple Guide
In
pytest, you can mock a function's return value using unittest.mock.patch or unittest.mock.Mock. Use patch as a decorator or context manager to replace the target function and set its return_value to the desired output.Syntax
Use unittest.mock.patch to replace a function or method during a test. Set the return_value attribute to specify what the mocked function should return.
@patch('module.function'): Decorator to mockfunctioninmodule.mock_function.return_value = value: Sets the return value of the mocked function.with patch('module.function') as mock_function:: Context manager alternative to decorator.
python
from unittest.mock import patch @patch('module.function') def test_example(mock_function): mock_function.return_value = 'mocked value' result = module.function() assert result == 'mocked value'
Example
This example shows how to mock the return value of a function get_data in a module data_source. The test replaces get_data to return a fixed value, allowing you to test code that depends on it without calling the real function.
python
from unittest.mock import patch import pytest # Imagine this is in data_source.py def get_data(): return 'real data' # Function under test def process_data(): data = get_data() return data.upper() # Test with mocked return value @patch('__main__.get_data') def test_process_data(mock_get_data): mock_get_data.return_value = 'mocked data' result = process_data() assert result == 'MOCKED DATA' if __name__ == '__main__': pytest.main([__file__])
Output
============================= test session starts ==============================
collecting ... collected 1 item
test_mock_return_value.py::test_process_data PASSED [100%]
============================== 1 passed in 0.01s ===============================
Common Pitfalls
Common mistakes when mocking return values in pytest include:
- Mocking the wrong import path. Always patch where the function is used, not where it is defined.
- Forgetting to set
return_value, so the mock returnsMagicMockinstead of the expected value. - Not using
patchas a decorator or context manager, causing the mock to not apply during the test.
python
from unittest.mock import patch # Wrong patch location (patching the function's original module) @patch('data_source.get_data') # This may fail if process_data imports differently def test_wrong_patch(mock_get_data): mock_get_data.return_value = 'mocked' result = process_data() assert result == 'MOCKED' # This might fail # Correct patch location (patch where process_data uses get_data) @patch('__main__.get_data') def test_correct_patch(mock_get_data): mock_get_data.return_value = 'mocked' result = process_data() assert result == 'MOCKED'
Quick Reference
Remember these tips when mocking return values in pytest:
- Use
patch('target_path')wheretarget_pathis where the function is used. - Set
mock.return_valueto control what the mock returns. - Use
patchas a decorator or context manager to apply the mock only during the test. - Check your import paths carefully to avoid patching the wrong place.
Key Takeaways
Use unittest.mock.patch to replace functions and set their return_value in pytest tests.
Always patch the function where it is used, not where it is defined, to ensure the mock works.
Set mock.return_value explicitly to avoid unexpected MagicMock objects.
Use patch as a decorator or context manager to limit the mock's scope to the test.
Verify your mock paths and test outputs to catch common mocking mistakes early.