Skip to content
Foxipa
Code Completion 4 Challenges

Complete the unittest.mock code

Practise controlled return values, sequential outcomes, scoped patching, and autospecced collaborators.

Intermediate ~12 min read

Challenge 1

Configure the lookup mock to return a known product mapping, then check the lookup interaction and calculated total.

python Your answer
import unittest
from unittest.mock import Mock

def double_price(product_id, lookup):
    return lookup(product_id)["price"] * 2

class TestDoublePrice(unittest.TestCase):
    def test_known_price(self):
        lookup = Mock(={"price": 6})
        result = double_price("sku-3", lookup)
        lookup.assert_called_once_with("sku-3")
        self.assertEqual(result, 12)

if __name__ == "__main__":
    unittest.main(argv=["exercise"], exit=False)

Press Ctrl⌘Enter to check your answer

Challenge 2

Supply one timeout followed by a successful result to test the retry logic without waiting on a real service.

python Your answer
import unittest
from unittest.mock import Mock

def call_with_one_retry(operation):
    try:
        return operation()
    except TimeoutError:
        return operation()

class TestRetry(unittest.TestCase):
    def test_retries_once(self):
        operation = Mock(side_effect=)
        self.assertEqual(call_with_one_retry(operation), "ready")
        self.assertEqual(operation.call_count, 2)

if __name__ == "__main__":
    unittest.main(argv=["exercise"], exit=False)

Press Ctrl⌘Enter to check your answer

Challenge 3

Patch the attribute that total_in_euros actually looks up, keeping the patch limited to the test block.

python Your answer
import unittest
from types import SimpleNamespace
from unittest.mock import patch

def real_rate(currency):
    raise RuntimeError("real service must not run")

gateway = SimpleNamespace(fetch_rate=real_rate)
def total_in_euros(amount):
    return amount * gateway.fetch_rate("EUR")

class TestTotal(unittest.TestCase):
    def test_uses_a_controlled_rate(self):
        with patch.object(gateway, , return_value=1.25) as fetch:
            self.assertEqual(total_in_euros(8), 10.0)
            fetch.assert_called_once_with("EUR")

if __name__ == "__main__":
    unittest.main(argv=["exercise"], exit=False)

Press Ctrl⌘Enter to check your answer

Challenge 4

Create a signature-aware mock of publish that returns "queued"; then verify the call made by enqueue_notice.

python Your answer
import unittest
from unittest.mock import create_autospec

def publish(channel, message, *, urgent=False):
    return "sent"

def enqueue_notice(sender):
    return sender("ops", "backup complete", urgent=True)

class TestEnqueueNotice(unittest.TestCase):
    def test_publisher_call(self):
        sender = 
        result = enqueue_notice(sender)
        self.assertEqual(result, "queued")
        sender.assert_called_once_with("ops", "backup complete", urgent=True)

if __name__ == "__main__":
    unittest.main(argv=["exercise"], exit=False)

Press Ctrl⌘Enter to check your answer