Skip to main content

9 posts tagged with "mock"

mock tag description

View All Tags

When to mock

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

The final act of mastering testing isn't just knowing how to mock, but understanding when to mock. Over-mocking can lead to brittle tests that provide a false sense of security, while a lack of mocks can make your test suite slow and unreliable. This article will conclude our series by clarifying the different types of test doubles and providing a robust framework for when to use each one, including when a real dependency is the better choice.

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 __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.

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].

Create OpenAPI mock server

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

Creating a mock server from a Swagger/OpenAPI file is a powerful way to decouple front-end and back-end development. It allows front-end developers to start building and testing their applications against a realistic API without waiting for the back-end to be completed.

Here's a step-by-step guide on how to set up a simple mock server using your OpenAPI specification file.