Reference library

Testing & modern typing

pytest basics, mocks, type hints, TypedDict, Protocol, and static-checking patterns.

2 matches
Testing & modern typing medium

How to Flag Unexpected Diff Changes in Python

Compares two snapshot lists, detects unexpected differences, and returns a flag indicating whether the snapshot should be updated.

diffing snapshot-testing difflib
Python
import difflib

def snapshot_diff(before, after, intentional_changes=None):
    """Compare snapshots and flag only unexpected differences."""
    intentional_changes = intentional_changes or set()
    diff = list(difflib.unified_diff(before, after, lineterm=""))
    has_unexpected = False

    for line in diff:
      …
16 0 Open
Testing & modern typing medium

How to Use Hypothesis Strategies for Lists of Text in Python

Generate random lists of non-empty strings with Hypothesis and verify that joining them with a comma-and-space separator meets expected length and containment invariants.

hypothesis property-based-testing strategies
Python
from hypothesis import given, strategies as st
from hypothesis import example


@given(st.lists(st.text(min_size=1, max_size=10), min_size=1, max_size=5))
def test_joined_string_length(items):
    """Each text is non-empty; a joined string should be at least as long
    as the number of items (separator adds character…
13 0 Open

Browse by section

Each section groups closely related Python snippets.

Testing & modern typing — Python code examples

What you will find here

This page collects testing & modern typing snippets — short, copy-ready Python you can paste into our free online IDE and run without installing anything. Each sample includes a plain-English explanation and the full source code.

Samples vs tutorials and challenges

Samples are quick reference — one concept per page. For step-by-step teaching, use our Python tutorials. To test yourself, try quizzes or coding challenges. Clean up style with the Python formatter.