Skip to main content

2 posts tagged with "mocking"

mocking tag description

View All Tags

Python Mocking: The Ultimate Guide from Basics to Advanced Test Double Patterns

· 7 min read
Serhii Hrekov
Senior Software Engineer & System Architect specializing in Python, Web Systems, Cloud Infrastructure & Automation

Unit tests must execute in complete isolation from external resources like databases, networks, and file systems. Python's built-in unittest.mock library and the pytest-mock plugin are the standard tools for creating test doubles, enabling you to inspect invocations, mock complex class initializers, verify function signatures, and control time.

This guide provides a comprehensive manual on Python mocking: foundational concepts, interface enforcement via spec/autospec, stateful simulations with side_effect, class constructor and instance method patching, Pytest mocker fixture workflows, asynchronous coroutines, and time freezing.

Python Mocking Guide: External Dependencies, Test Doubles, and Pytest Best Practices

· 9 min read
Serhii Hrekov
Senior Software Engineer & System Architect specializing in Python, Web Systems, Cloud Infrastructure & Automation

External dependencies are a major source of pain in unit testing. Network calls, persistent databases, and file systems are inherently slow, non-deterministic, and prone to environmental failures. Mocking these boundaries isolates your unit tests and delivers sub-second test execution suites.

However, over-mocking leads to brittle tests that pass while production code breaks. This guide provides a complete manual on Python test doubles: the architectural framework of when to mock, test double taxonomy (stubs, fakes, spies, mocks), hands-on unittest.mock examples for HTTP APIs, databases, and file systems, plus Pytest mocking pitfalls to avoid.