Mocking (unittest.mock)

say it: “YOO-nit-test dot MOK” /ˈjunɪtˌtɛst dɑt ˈmɑk/ or “MOK-ing” /ˈmɑkɪŋ/

Swapping a real part of the program for a fake one during a test.

What is mocking in testing?

Mocking swaps a real piece of your program, like a payment service, for a fake one during a test, so the test is fast and never charges a real card.

from unittest.mock import Mock

def checkout(payments, total):
    return payments.charge(total)

fake_payments = Mock()
fake_payments.charge.return_value = "approved"
assert checkout(fake_payments, 50) == "approved"

Level: Freelancer · More Python terms at this level

Updated