Skip to main content

3 posts tagged with "unittest"

unittest tag description

View All Tags

Python mocking frameworks

· 7 min read
Serhii Hrekov
software engineer, creator, artist, programmer, projects founder

Mocking becomes particularly valuable when testing code that interacts with complex frameworks and libraries. While the core concepts of unittest.mock remain the same, the specific objects and functions you need to patch can vary. This article will provide practical, in-depth examples of how to apply mocking to three common Python ecosystems: web frameworks (Django/Flask), asynchronous programming (asyncio), and data science (pandas).

Mocking Time and Dates in Python

· 7 min read
Serhii Hrekov
software engineer, creator, artist, programmer, projects founder

Mocking time and dates is a common and often essential practice when writing tests for time-dependent logic. If your code's behavior changes based on the current time (e.g., a function that checks for a subscription's expiration, a daily report generator, or a cache invalidation rule), relying on the system's clock will lead to unreliable and non-repeatable tests.

You need a way to "freeze" time or fast-forward it to a specific point so your tests run in a controlled, predictable environment. Python's unittest.mock.patch is the perfect tool for this, allowing you to replace datetime.now() with a mock that returns a fixed value.