Testing & modern typing
pytest basics, mocks, type hints, TypedDict, Protocol, and static-checking patterns.
How to Load Test a Local API with Locust in Python
Defines a Locust load test that simulates traffic to local endpoints, enabling manual load testing against a development server.
from locust import HttpUser, task, between
class WebsiteUser(HttpUser):
wait_time = between(1, 3)
@task
def home_page(self):
self.client.get("/")
@task(3)
def about_page(self):
self.client.get("/about")
if __name__ == "__main__":
print("Run with: locust -f this_file.py --h…
How to Mock and Stub API Calls in Playwright E2E Tests with Python
This code demonstrates how to mock and stub API responses in Playwright end-to-end tests using Python's unittest.mock patch and Playwright's APIRequestContext.
import re
from unittest.mock import patch
from playwright.sync_api import sync_playwright
def verify_api_mock(page, mock_url, mock_response):
with patch("playwright.sync_api.APIRequestContext.get") as mock_get:
mock_get.return_value.json.return_value = mock_response
mock_get.return_value.status_co…
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.