Skip to main content

9 posts tagged with "testing"

testing tag description

View All Tags

Python Mocking Guide: When to Mock, Test Doubles, and Pytest Pitfalls

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

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: The Ultimate Guide from Basics to Advanced Test Double Patterns

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

Unit tests must run in complete isolation from external resources like databases, networks, and file systems. Python's unittest.mock library is the standard tool 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, covering foundational concepts, interface enforcement via specs, stateful simulation, constructor patching, temporal mocking, and ecosystem integrations.

Pytest: Mocking Objects and Classes

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

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
Senior Software Engineer & System Architect specializing in Python, Web Systems, Cloud Infrastructure & Automation

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.

Given-When-Then vs. Alternative Python Test Structuring Patterns

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

For docstrings in Python tests, there isn't a single technique that's universally "better" than Given-When-Then. The best technique depends on your project's needs, your team's familiarity with different styles, and the specific type of testing you're doing.

Python Doctests: The Complete Guide to Documentation-Driven Testing

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

Writing documentation is essential, but code examples in documentation frequently rot over time as codebases evolve. Python's built-in doctest module solves this problem by allowing you to write executable code examples directly inside your docstrings. The system verifies that the actual function output matches your documented example output.

By combining doctest (for living documentation correctness) and pytest (as your primary test runner), you get a symbiotic testing strategy that guarantees your code works and your documentation is always truthful.

This guide covers when to use doctests, how to write them, the various ways to execute them, and how to optimize your developer workflow in VS Code and Gitpod.

Testing in Python for Beginners. Using `unittest` and `pytest` with Fun Examples

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

Writing tests in Python helps ensure that your code works correctly. In this guide, we'll use two popular testing tools: the built-in unittest module and the third-party library pytest. We'll walk through both using examples related to vegetables and AI model names.

Paradigms Every Beginner Should Know Before Learning Shift Left

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

Before diving into Shift Left, which emphasizes catching bugs, performance, and security issues early in the software development lifecycle, it's important for new programmers to learn the foundational paradigms that support this philosophy.

These paradigms teach early thinking, good code hygiene, and automation - all of which are building blocks of effective software engineering.