惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
人人都是产品经理
人人都是产品经理
Cisco Talos Blog
Cisco Talos Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
V
V2EX
博客园 - 三生石上(FineUI控件)
Martin Fowler
Martin Fowler
WordPress大学
WordPress大学
D
Docker
S
SegmentFault 最新的问题
博客园 - 聂微东
美团技术团队
Apple Machine Learning Research
Apple Machine Learning Research
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Last Week in AI
Last Week in AI
M
MIT News - Artificial intelligence
F
Fortinet All Blogs
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
The GitHub Blog
The GitHub Blog
GbyAI
GbyAI
L
LangChain Blog
Vercel News
Vercel News
博客园 - 叶小钗
MongoDB | Blog
MongoDB | Blog
Stack Overflow Blog
Stack Overflow Blog
H
Help Net Security
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
The Cloudflare Blog
Engineering at Meta
Engineering at Meta
T
Threat Research - Cisco Blogs
T
Threatpost
Scott Helme
Scott Helme
T
Tailwind CSS Blog
Latest news
Latest news
Stack Overflow Blog
Stack Overflow Blog
Blog — PlanetScale
Blog — PlanetScale
The Register - Security
The Register - Security
罗磊的独立博客
P
Proofpoint News Feed
腾讯CDC
S
Schneier on Security
雷峰网
雷峰网
A
About on SuperTechFans
T
Tenable Blog
F
Full Disclosure
Cyberwarzone
Cyberwarzone
博客园_首页
有赞技术团队
有赞技术团队
K
Kaspersky official blog

DEV Community

We Trusted Auto-Ack. The Queue Agreed. Our Costs Didn't. DevOps for Developers: Reducing Cognitive Load and Boosting Transparency Next.js SaaS Boilerplate with BetterAuth, RBAC, i18n & Production-Ready Setup I built a free streaming site from scratch — no ads, no framework, no BS Beyond Static Prompts: How to Build Self-Improving AI Agents with Closed-Loop Skill Playbooks How I Taught My Incident Alerts to Say "This Broke 3 Minutes After Your Last Deploy" Why I Stopped Treating Job Applications as My Only Career Strategy Stop Watching Tutorials, Start Coding: How I Built CodeQuizz, an AI-Powered Active Learning Engine How We Generate 300+ AI Business Ideas a Month With GPT-5 (and Filter the Junk Out) The Intent Layer Your AI Coding Agent Does Not Need a Bigger Prompt How I solved a problem in my house using with an AI-powered application! Structure: A Local-First Interview IDE Powered by Gemma 4 Build in public, month 2: 615 of 616 visitors never clicked anything Someone wrote a fake EULA into Bitcoin. Two hours later they revoked it. Insights of Git ( part : 1 ) Someone wrote a fake EULA into Bitcoin. Two hours later they revoked it. Payload CMS Has 508 Circular Dependencies. Next.js Has 17. Here's Why They Form in Every Large JS Codebase. Prompt Packs Are Dead. Long Live Skills Why I Started Building a Portfolio Tracker Senior developer" after 3 years is title laundering Stripe Webhook Idempotency in FastAPI: Handling Duplicate Events Without Double-Charging SaaS Customers What Happens Before Your C Program Reaches the CPU? FinOps for Startups: How to Keep Your AWS Bill Under $100/Month Configuring CORS in Azure API Management How RBI Quietly Created a New Billion Dollar Industry in International Payments Time Need To Rearrange Binary String I Updated My GitHub Auto-Commit Desktop App I Have Reviewed Over 400 Resumes for Tech roles. Here Is What Actually Gets You the Phone Screen [Boost] Awesomeness! We built a lightweight, 100% local File Integrity Monitor (FIM) with zero telemetry Building chart() for Tala: From Raw Indicator Data to Something You Can Actually Inspect A client-side secret scanner that physically can't exfiltrate your code (and why you shouldn't trust mine either) Your AI Agent Should Text You First Built free app for game design and worldbuilding You Have a Free AI Model Sitting in Chrome Right Now I created a fork of GunDB and rewrote it in TypeScript using Vibe Code 6 Advanced JavaScript Questions That Separate Seniors from Mid-Levels Claude Does Not Need More Prompts. It Needs Reasoning Discipline. An Introduction to AI Hub, Part 2: Custom MCP Servers I built a RAG pipeline from scratch — no LangChain, just FastAPI + FAISS How I built a dependency risk scanner with Coral in 7 days Local-first: a Model on Your Own Machine, Zero Cloud 2487. Remove Nodes From Linked List C_STD : A Leak-Free, Cross-Platform Standard Library for Modern C How to build your professional network as a developer — authentic strategies The Pope and the Dynamo Building ShouldWeAutomate: A Decision Intelligence Platform for Workflow Automation The Reputation Layer: Why Developers Quietly Run Corporate PR The Last Mile of Software Is a Sentence AppView 1.0.0 Released: Instrument and Secure Your LLM Deployments The Hermes Rescue: How an Open Agent Rebuilt My GitHub Projects from Scratch S2 — Heap Corruption Crashes: How to Diagnose and Fix Them I built a Chrome extension because I couldn't stop opening Twitter between Pomodoro sessions AI cheating in technical interviews is invisible to interviewers — here's how we detect it Lean4 Might Be the Missing Piece in AI: Why Theorem Provers Are Suddenly Everywhere The Zero-Drift API Series: Stop Trusting a Green Build You Can't Explain How I Deployed My First Project on AWS (And Didn't Break Everything) How I Built a Real-Time Quiz Platform with Next.js, WebSockets, and Learning Science When Your VPS Blocks Outbound SMTP: What Actually Helps Los agentes de código necesitan memoria durable, no solo contexto Cognitive Architectures of AGI: 7 Patterns That Transform LLMs from Oracles into Thinkers I Built a Chat App That Deletes Itself (Because I Was Bored at 2am) Uncovering the Power of Linux's History Command How to Add a Contact Form to Your Ghost Blog Accept Payments in Minutes with Afriex Checkout Sessions Hermes Agent Gets Smarter Every Day. So Does the Bill. How I get Next.js sites to load almost instantly — a practical checklist Treasure Hunt Engine: Why One Bad Prometheus Rule Sank the Whole Veltrix Event Test a DNS Leak in 2 Minutes: Complete Methodology + Per-OS Fixes (2026) Lessons from building a Chrome extension Rivet: A library i made in 2 days I Built a Speech-to-Text Tool Because Sometimes Typing Just Gets in the Way How I'm Building a Multi-Agent Crew for AI Coding Supervision (Cipher Update) Your AI Agent Needs a Manager, Not a Superhero I Built CausalLens — A Free, Open-Source Causal Impact Calculator for Time Series (5 Methods, Zero Setup) How to write good commit messages and pull requests — a team guide Cipher: The Jarvis with a Hermes Core How to build a second brain with Obsidian and Claude Code (step by step) Claude completed my MPI assignment. Then it couldn't run it. So I built the missing piece. This 100% How Our Document Ingestion Pipeline Turns Files into LLM-Ready Markdown Agentic AI Model Risk Management: Aligning with Regulatory Expectations CTV Fraud Has an IPv6 Business Problem The great AI enshittification The Veltrix Treasure Hunt Engine: Why Our First Rewrite Cost Us 3.2 Million Requests Per Second I Made My AI Models Argue, Then Let Hermes Be the Judge Road To KiwiEngine #4: The Racecar Driver Analogy Run Aider on Ollama, Bedrock, or Any LLM Provider — One Gateway, Every Model BAIXAR VÍDEO DO YOUTUBE Releasing HeliosProxy, The programmable Postgres data-plane Hello, DEV Community! 👋 Three Bitcoin Primitives That Don't Exist Anywhere Else (PoW Beacon, DLC Oracle, Fair-Launch Rune) Append-only doesn't mean what you'd hope Notes from the Mistral AI Now Summit Are Claude skills safe in 2026? What the Snyk ToxicSkills audit actually found How to not Lose $500M via API Bills: Run Private AI for 100 Engineers Under $1 Million The Unlikely Journey from Bricks to Bytes Three TODOs, three weeks, one weekend: finishing pq v0.14
Python pytest: Write Tests That Actually Help You
German Yamil · 2026-05-31 · via DEV Community

Why Testing Matters (and Why pytest)

Tests catch bugs before users do. pytest makes writing them fast enough that you'll actually do it.

pip install pytest

Enter fullscreen mode Exit fullscreen mode

Run with: pytest (auto-discovers test_*.py files)

Basic Test Structure

# test_calculator.py

def add(a: int, b: int) -> int:
    return a + b

def test_add_positive():
    assert add(2, 3) == 5

def test_add_negative():
    assert add(-1, -1) == -2

def test_add_zero():
    assert add(0, 5) == 5

Enter fullscreen mode Exit fullscreen mode

Run: pytest test_calculator.py -v

test_calculator.py::test_add_positive PASSED
test_calculator.py::test_add_negative PASSED
test_calculator.py::test_add_zero     PASSED
3 passed in 0.01s

Enter fullscreen mode Exit fullscreen mode

Testing Exceptions

import pytest

def divide(a: float, b: float) -> float:
    if b == 0:
        raise ValueError("Cannot divide by zero")
    return a / b

def test_divide_by_zero():
    with pytest.raises(ValueError, match="Cannot divide by zero"):
        divide(10, 0)

def test_divide_normal():
    assert divide(10, 2) == 5.0

Enter fullscreen mode Exit fullscreen mode

Fixtures: Reusable Setup

Fixtures provide test data or objects without repeating setup code:

import pytest

class UserDB:
    def __init__(self):
        self.users = {}

    def add_user(self, name: str, email: str):
        self.users[email] = {"name": name, "email": email}

    def get_user(self, email: str) -> dict | None:
        return self.users.get(email)

@pytest.fixture
def db():
    """Fresh database for each test."""
    return UserDB()

@pytest.fixture
def db_with_user(db):
    """Database pre-loaded with a test user."""
    db.add_user("Alice", "alice@example.com")
    return db

def test_add_user(db):
    db.add_user("Bob", "bob@example.com")
    assert db.get_user("bob@example.com")["name"] == "Bob"

def test_get_existing_user(db_with_user):
    user = db_with_user.get_user("alice@example.com")
    assert user["name"] == "Alice"

def test_get_missing_user(db):
    assert db.get_user("nobody@example.com") is None

Enter fullscreen mode Exit fullscreen mode

Each test gets a fresh db — no state leaks between tests.

Parametrize: Test Multiple Inputs

import pytest

@pytest.mark.parametrize("a, b, expected", [
    (2, 3, 5),
    (-1, 1, 0),
    (0, 0, 0),
    (100, -50, 50),
])
def test_add(a, b, expected):
    assert add(a, b) == expected

Enter fullscreen mode Exit fullscreen mode

One test function, four test cases. pytest runs and reports each separately.

Mocking with monkeypatch

import requests

def get_user_name(user_id: int) -> str:
    r = requests.get(f"https://api.example.com/users/{user_id}")
    return r.json()["name"]

def test_get_user_name(monkeypatch):
    class FakeResponse:
        def json(self):
            return {"name": "Alice", "id": 1}

    def fake_get(url):
        return FakeResponse()

    monkeypatch.setattr(requests, "get", fake_get)

    result = get_user_name(1)
    assert result == "Alice"

Enter fullscreen mode Exit fullscreen mode

The real requests.get never fires — your test runs offline and fast.

Using pytest-mock for Cleaner Mocks

pip install pytest-mock

Enter fullscreen mode Exit fullscreen mode

def test_get_user_name(mocker):
    mock_get = mocker.patch("requests.get")
    mock_get.return_value.json.return_value = {"name": "Alice"}

    result = get_user_name(1)

    assert result == "Alice"
    mock_get.assert_called_once_with("https://api.example.com/users/1")

Enter fullscreen mode Exit fullscreen mode

Test Organization

project/
├── src/
│   └── myapp/
│       ├── __init__.py
│       ├── calculator.py
│       └── users.py
└── tests/
    ├── conftest.py        # Shared fixtures
    ├── test_calculator.py
    └── test_users.py

Enter fullscreen mode Exit fullscreen mode

conftest.py — fixtures available to all test files without importing:

# tests/conftest.py
import pytest
from myapp.users import UserDB

@pytest.fixture
def empty_db():
    return UserDB()

@pytest.fixture
def populated_db(empty_db):
    empty_db.add_user("Alice", "alice@example.com")
    empty_db.add_user("Bob", "bob@example.com")
    return empty_db

Enter fullscreen mode Exit fullscreen mode

Useful pytest Options

pytest                    # Run all tests
pytest -v                 # Verbose output
pytest -k "test_add"      # Run tests matching pattern
pytest --tb=short         # Shorter tracebacks
pytest -x                 # Stop on first failure
pytest --lf               # Run only last-failed tests
pytest -s                 # Show print() output
pytest --cov=src          # Coverage report (needs pytest-cov)

Enter fullscreen mode Exit fullscreen mode

What to Test

Test these:

  • Happy path (valid inputs, expected output)
  • Edge cases (empty list, zero, None, empty string)
  • Error conditions (invalid input raises exception)
  • Boundary values (min/max for numeric inputs)

Don't test:

  • Python itself (assert 1 + 1 == 2)
  • Third-party library internals
  • Private implementation details that change often

Further Reading


Get the Full Pipeline

This article is part of the Python AI Publishing Pipeline series — a complete system to write, validate, and publish technical ebooks with Python and Claude.

📋 Free checklist: 7 steps to ship a Python ebook — PDF, no email required.

🚀 Full pipeline + source code: germy5.gumroad.com/l/xhxkzz — $9.99, 30-day money-back guarantee.


If this was useful, the ❤️ button helps other developers find it.

Building a Python content pipeline? I sell the complete automation system as a one-time download — Dev.to API, Claude API, launchd, Gumroad. Check it out ($9.99)