The independent file on technology, travel & lifeFrom Pakistan, for everywhere
Huzi Blogs.
The ArchiveSectionsSearch

Planning a trip? HTG Travels — flights, visas, hotels & Umrah, handled properly.

WhatsApp +92 317 5054959
Huzi Blogs.

An independent file on technology, travel and life — long-form guides written plainly, from Pakistan, for everywhere.

ShopMeet the Creator

Sections

  • Travel
  • Technology
  • Lifestyle
  • Programming
  • Sports
  • Linux
  • All sections

The Desk

  • The Archive
  • Search the file
  • Disclaimer & Data Privacy Policy
  • A project of huzi.pk

© 2026 blogs.huzi.pk — All rights reserved.

Set in Playfair Display & Inter

    HomeProgrammingStory
    Programming

    Python Testing Pyramid 2026: The Complete Problem-Solving Guide

    H

    By Huzi

    File #560 · 4 min read

    The Friday bug ships not because a team had no tests, but because the integration test had been silently skipping itself for three weeks while CI glowed green. Bad test architecture is the wrong tests in the wrong layer — no tooling fixes that.

    The quick answer: shape the suite as roughly 70% unit, 20% integration, 10% end-to-end, and keep the inner loop under five seconds so people run it. pytest, pytest-asyncio, Hypothesis and testcontainers carry every layer — they need the right shape.

    The 70/20/10 shape

    Roughly seventy percent unit tests: fast, isolated, every external dependency mocked, each done in under a second. Twenty percent integration: a real database and real schemas, catching what mocks cannot.

    Ten percent end-to-end is reserved for flows that must never break — signup, checkout, login. The inverted pyramid is the common failure: a 45-minute browser suite that flakes and teaches everyone to ignore red.

    Layer Share What lives there Speed target
    Unit ~70% Isolated logic, dependencies mocked Under a second each
    Integration ~20% Real database, real schemas Seconds
    End-to-end ~10% Signup, checkout, login Minutes, nightly

    Fast tests get run. Slow tests get skipped. Hold the line.

    pytest carries the load

    Plain assert statements, fixtures for setup and teardown, parametrize for data-driven cases, markers for filtering. A fixture uses @pytest.fixture; yield instead of return, and code after the yield is teardown that runs even when the test raises.

    import pytest
    
    @pytest.fixture
    def sample_user() -> dict:
        return {"id": 1, "name": "Huzi", "email": "[email protected]"}
    
    @pytest.mark.parametrize("a, b, expected", [
        (2, 3, 5), (-1, 1, 0), (0, 0, 0),
    ])
    def test_add(a: int, b: int, expected: int) -> None:
        assert add(a, b) == expected
    

    Shared fixtures live in conftest.py, discovered automatically. Register custom markers in pyproject.toml, then run pytest -m "not slow" for the fast loop and pytest -m slow nightly — the inner loop survives and the expensive paths still run.

    Mock the boundaries, never the internals

    Mock what your code does not control: external APIs, the clock, the filesystem. Mocking internal collaborators couples tests to the implementation, so every refactor breaks them for nothing.

    unittest.mock.patch is the standard tool; pytest-mock's mocker fixture is the ergonomic wrapper that cleans up after itself.

    def test_get_user_ip(mocker) -> None:
        mock_get = mocker.patch("myapp.api.requests.get")
        mock_get.return_value.json.return_value = {"ip": "203.0.113.7"}
        assert get_user_ip() == "203.0.113.7"
        mock_get.assert_called_once_with("https://api.ipify.org")
    

    assert_called_once_with is the safety net: not just "was it called" but "with exactly these arguments" — how you prove the payment gateway got the right order ID without a real gateway. freezegun pins datetime.now() and turns flaky time-dependent tests deterministic.

    Boundaries deserve the same suspicion in travel paperwork: a passport six months short or a transit rule misread is a runtime error at the airport. HTG Travels verifies real requirements before you book.

    The layers that catch real bugs

    Hypothesis generates hundreds of cases from one function — empty lists, huge integers, Unicode you would never type — and shrinks any failure to the smallest input that still fails. Three properties pin down a sort completely: output is sorted, length is preserved, no element invented.

    testcontainers spins up a real PostgreSQL in a throwaway Docker container, so repository tests catch the schema drift SQLite politely hides. In CI, pytest -n auto --cov=src --cov-report=html parallelizes across cores and hands you a coverage report.

    Pro tip: Turn branch = True on in coverage config — line coverage lies about an if that only ran one way.

    Verification is an act of respect, and I think of Gaza's students sitting exams in tents — may the world one day grade itself as honestly as a good test suite.

    Before we submit any visa file, someone re-checks every page against the checklist — passport validity, photo size, the meningococcal certificate — because an error caught at the desk costs nothing and one at the embassy costs the trip.

    That is the discipline HTG Travels runs on — bring us the file refused elsewhere; finding out why is our favourite first step. When the deploy can wait and the mountains cannot: +92 317 5054959.

    Presented by

    HTG Travels — Plan the Trip, Skip the Runaround

    Flights, visas, Umrah packages and Hunza–Skardu tours, handled end to end from our licensed Sialkot desk. Real fares, hotel names in writing, and a human who answers — before you pay a rupee.

    Flights & rebooking Visa files Umrah 2026–27 Northern tours
    Visit htg.com.pkWhatsApp +92 317 5054959

    Sialkot, Pakistan · Licensed agency · Fast quotes on WhatsApp


    You Might Also Like

    Luxury 3D Handwork Heavy Embroidered Net Bridal Saree | 6 Yards Pallu

    Luxury 3D Handwork Heavy Embroidered Net Bridal Saree | 6 Yards Pallu

    PKR 9600

    Handwork Embroidered Net Wedding Suit 2025 | Luxury Net Shawl & Jamawar Trouser

    Handwork Embroidered Net Wedding Suit 2025 | Luxury Net Shawl & Jamawar Trouser

    PKR 6999

    Decent Black Velvet Suit (9000 Micro) | Embroidered Organza Dupatta

    Decent Black Velvet Suit (9000 Micro) | Embroidered Organza Dupatta

    PKR 7350

    Digital Print 90/70 Lawn 3-Piece Suit with Diamond Voil Dupatta (Unstitched)

    Digital Print 90/70 Lawn 3-Piece Suit with Diamond Voil Dupatta (Unstitched)

    PKR 3800

    High-Quality Digital Print Lawn Suit 3-Piece with Diamond Voil Dupatta (90/70)

    High-Quality Digital Print Lawn Suit 3-Piece with Diamond Voil Dupatta (90/70)

    PKR 3900

    Keep reading

    Related files

    Programming

    Python Async/Await Deep Dive 2026: The Complete Problem-Solving Guide

    asyncio.run, gather, TaskGroup, httpx sessions and the blocking-call escape hatch — the 2026 Python async patterns that actually ship, with copy-paste code.

    By HuziFile #536

    Read
    Programming

    Database Design Fundamentals 2026: The Complete Problem-Solving Guide

    Normalization to stop the data lying, composite and partial indexes for the queries you actually run, NUMERIC for money, and zero-downtime migrations with expand-contract.

    By HuziFile #550

    Read
    Programming

    Go for Node.js Developers 2026: The Complete Problem-Solving Guide

    Goroutines, channels, error values and net/http — the parts of Go that fix what blocks Node in production, with side-by-side JS and Go comparisons.

    By HuziFile #544

    Read
    Programming

    Rust for JavaScript Developers 2026: The Complete Problem-Solving Guide

    Ownership, borrowing, Option, Result and the ? operator, Tokio and a first Axum API — Rust explained through the JavaScript habits you already have.

    By HuziFile #538

    Read
    Programming

    Redis Caching Mastery 2026: The Complete Problem-Solving Guide

    Cache-aside, TTL discipline, stampede locks, sliding-window rate limits, SCAN over KEYS, and when Sentinel beats Cluster — the Redis patterns that fix real latency.

    By HuziFile #534

    Read
    Programming

    TypeScript Advanced Patterns 2026: The Complete Problem-Solving Guide

    Conditional types with infer, mapped types, branded primitives, satisfies, and exhaustive discriminated unions — the patterns that move bugs from runtime to compile time.

    By HuziFile #532

    Read
    Back to the archive