# 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.

```python
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](https://www.ask-codey.com/glossary?category=python&level=4)

Updated: November 17, 2026

Source: https://www.ask-codey.com/glossary/mocking-unittest-mock
