Middle
Have you worked with mocks/stubs?
sobes.tech AI
Answer from AI
Yes, I have worked with mocks and stubs in test automation.
Mock is an object that simulates the behavior of a real object and allows checking how the tested code interacts with it (for example, method calls, passed parameters).
Stub is a simplified implementation of a component that returns predefined data to isolate the tested code from external dependencies.
Example of using mock in Python with the unittest.mock library:
from unittest.mock import Mock
def fetch_data(api_client):
return api_client.get_data()
mock_api = Mock()
mock_api.get_data.return_value = {'key': 'value'}
result = fetch_data(mock_api)
assert result == {'key': 'value'}
mock_api.get_data.assert_called_once()
Using mock/stub helps to:
- Isolate the tested code from external services and complex dependencies.
- Verify interactions and calls.
- Speed up test execution.
In QA Automation, this is a standard practice for creating reliable and fast tests.