Skip to main content

7 posts tagged with "testing"

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

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.