What is mocking, and how do you do it in Python tests?

Python data, testing and performance · Freshers and experienced

Model answer

Replacing a real dependency (network, database, clock) with a fake during a test. unittest.mock.patch swaps an object temporarily, and pytest's monkeypatch fixture changes attributes or environment variables. Mock at the boundary, and avoid mocking the code you are testing.

Learn this topic

Mini Project: Analyse a CSV File and Make a Report in the course Python for Beginners: Learn to Code with Real Projects.

Sign in to add your own answer.

More questions