Python examples of using @patch as decorator
- User
Python examples of using @patch as decorator
Assistant
In Python,
@patchis a decorator provided by theunittest.mocklibrary to replace a function or an object with a mock object during testing.Here is an example of using
@patchas a decorator:pythonimport unittest.mock as mock def my_function(): import requests response = requests.get('https://www.example.com') return response.status_code @mock.patch('my_function.requests.get') def test_my_function(mock_get): mock_get.return_value.status_code = 200 assert my_function() == 200 test_my_function()
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.