Test Overview
This test uses MagicMock to simulate a database call. It verifies that the mocked method is called once and returns the expected value.
This test uses MagicMock to simulate a database call. It verifies that the mocked method is called once and returns the expected value.
from unittest.mock import MagicMock import pytest class Database: def get_user(self, user_id): # Imagine this method talks to a real database pass def test_get_user_called_once(): db = Database() db.get_user = MagicMock(return_value={'id': 1, 'name': 'Alice'}) result = db.get_user(1) db.get_user.assert_called_once_with(1) assert result == {'id': 1, 'name': 'Alice'}
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts | Test environment is ready with pytest and unittest.mock imported | — | PASS |
| 2 | Create Database instance and replace get_user with MagicMock returning a user dict | Database instance created; get_user method mocked to return {'id': 1, 'name': 'Alice'} | — | PASS |
| 3 | Call mocked get_user method with argument 1 | get_user called with 1, returns {'id': 1, 'name': 'Alice'} | Check that get_user was called once with argument 1 | PASS |
| 4 | Assert that get_user was called exactly once with argument 1 | Mock records one call with argument 1 | db.get_user.assert_called_once_with(1) passes | PASS |
| 5 | Assert that the returned result matches the expected dictionary | Result is {'id': 1, 'name': 'Alice'} | assert result == {'id': 1, 'name': 'Alice'} passes | PASS |
Mock and MagicMock in pytest?Mock creates fake objects for testing, but does not automatically support Python's special methods like __len__ or __getitem__.MagicMock extends Mock by including default implementations for many special methods, making it useful when those methods are called during tests.MagicMock supports special methods like __len__ and __getitem__, Mock does not. -> Option AMagicMock object in pytest?MagicMock().MagicMock is a class, so MagicMock() creates a new mock object. The other options use invalid syntax for instantiation.from unittest.mock import MagicMock mock = MagicMock() mock.__len__.return_value = 5 print(len(mock))
mock.__len__.return_value = 5 tells the mock to return 5 when len() is called on it, because len() calls the __len__ method.print(len(mock))__len__ is mocked to return 5, len(mock) will print 5.from unittest.mock import Mock mock = Mock() mock.__getitem__.return_value = 'value' print(mock[0])
Mock does not implement special methods like __getitem__ automatically, so setting mock.__getitem__.return_value has no effect on indexing.mock[0] calls __getitem__, but since Mock does not handle it, this raises a TypeError. To mock indexing, use MagicMock.obj.__str__() internally. Which mock object and setup will correctly simulate str(obj) returning 'mocked string'?str(obj) call internally uses the __str__ method, which is a special method.MagicMock supports special methods like __str__. Setting mock.__str__.return_value = 'mocked string' makes str(mock) return 'mocked string'.mock = Mock() and set mock.__str__ = lambda: 'mocked string' uses Mock which does not support special methods by default. Options C and D use incorrect attribute names or types.mock = MagicMock() and set mock.__str__.return_value = 'mocked string' -> Option DPass the test and this pattern moves unknown