Explore Kartoza

Try searching for: QGIS, Training, GeoNode, PostGIS

Python Mocking Introduction
Back to Blog
Python Testing
January 3, 2021 Zulfikar Akbar Muzakki

Python Mocking Introduction

This comprehensive guide explores mocking in unit testing, focusing on isolating code from external dependencies.

Python Mocking Introduction

Python

Overview

This comprehensive guide explores mocking in unit testing, focusing on isolating code from external dependencies. The article demonstrates practical techniques using Python’s built-in unittest.mock module (available since Python 3.3).

Core Purpose of Mocking

The author explains that mocking allows developers to “isolate our code during the test, without having to worry about the unexpected behavior” of external dependencies like Firebase or third-party libraries.

Key Concepts Covered

1. Mock Module Basics

Introduction to unittest.mock and its capabilities for creating mock objects and functions

2. Practical Example

A SaveGrowthToFirebase class demonstrating real-world mocking scenarios

3. Mocking Strategies

  • Mocking date/time functions for consistent test results
  • Using @mock.patch() and @mock.patch.object() decorators
  • Mocking method calls and verifying parameters

4. Critical Pitfalls

  • Decorator Order Matters - Parameters map to reversed decorator order due to Python’s function definition mechanics
  • False Positive Prevention - Using autospec=True prevents mock objects from accepting incorrect parameters

5. Firebase Integration Testing

Mocking Firestore client calls to verify correct method invocations without actual database operations

Code Examples

The article includes complete, runnable code demonstrating:

  • How to mock the date function
  • Mocking instance methods with mock.patch.object()
  • Verifying mock calls with assertion methods like assert_called_once_with()
  • Chaining mock object calls for complex operations

Practical Benefits

The author emphasizes that proper mocking significantly reduces test execution time and prevents unnecessary data writes to external services, making tests more efficient and reliable.

Want to Learn More?

Explore our training courses or get in touch to discuss how we can help your organization.