Skip to main content

Mocking __init__ methods in Python

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

Patching a class's __init__ method is a common and powerful technique for unit testing code that relies on objects whose initialization performs unwanted side effects. These side effects can include making API calls, connecting to a database, or performing other time-consuming or stateful operations [1]. By mocking __init__, you can prevent these actions and verify that the class was instantiated correctly.

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.

Pytest mock pitfalls

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

Writing tests that are effective and maintainable is an art. While mocking is a powerful tool, it's not a silver bullet and can be misused. This final article of the mocking series will cover the best practices and common pitfalls of mocking in Python, ensuring your tests are robust, readable, and provide a high degree of confidence in your code.

Pytest: Mocking Objects and Classes

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

Mocking entire classes and objects is a crucial skill for unit testing complex code. When a function instantiates a class or calls methods on a passed-in object, you need a way to control that object's behavior without executing its real logic. This article will cover advanced mocking techniques, focusing on how to mock classes and instance methods using both the standard unittest.mock library and the more convenient pytest-mock plugin. We will also clarify key distinctions between different types of test doubles.

Mock external dependencies in Python unittest

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

External dependencies are a major source of pain in unit testing. They can be slow, unreliable, and difficult to control. Mocking these external services is the single most common and valuable use of the unittest.mock library [1]. This article will provide practical, hands-on examples for mocking three of the most frequent external dependencies: HTTP requests, database connections, and file system operations.

Mocking in Python

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

Mocking in Python is a fundamental practice for writing robust, reliable, and efficient tests. It allows developers to isolate a unit of code from its external dependencies, ensuring that a test is only concerned with the logic it is meant to verify [1, 2].

n8n, Make, or Zapier comparison table

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

Choosing the right workflow automation platform is a critical business decision that impacts everything from efficiency and cost to data security and technical control. There is no single "best" platform; the final verdict depends entirely on your specific use case, technical skills, and budget [1, 2].

n8n vs. Make (formerly Integromat)

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

n8n and Make (formerly Integromat) are two of the most powerful workflow automation platforms available, but they have fundamentally different approaches. While both allow you to build complex automations with a visual interface, they cater to different users and use cases. Choosing between them comes down to a trade-off between accessibility and raw technical power [1].

n8n for AI and LLM automations

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

As the automation landscape evolves, the integration of Artificial Intelligence (AI) and Large Language Models (LLMs) into workflows has become a game-changer. n8n is particularly well-suited for this, thanks to its open, flexible architecture that allows you to connect AI services to virtually any other tool in your stack [2]. This article will explore how to leverage n8n for AI and LLM automations, providing practical examples and use cases.

Foundation for building production-ready n8n automations

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

For mission-critical automations, simply creating a workflow is not enough. To ensure your n8n workflows are reliable, scalable, and secure, you must implement proper error handling, leverage custom code for advanced logic, and choose a robust deployment strategy. This article will cover these critical topics, setting the foundation for building production-ready n8n automations.

Looking for more content?
Hrekov Blog contains 165 articles. Browse the blog archive or Explore the full timeline.