Python API Testing Practice: Requests & Pytest Exercises
Write, execute, and debug Python API tests using requests and pytest directly in your browser. Validate status codes, extract nested JSON payloads, and test auth tokens with zero installation.
import requests
import pytest
BASE_URL = "https://targetqa.com/api/practice"
def test_login_and_fetch_profile():
# 1. Authenticate and retrieve token
res = requests.post(f"{BASE_URL}/auth/token", json={
"username": "qa@target.dev",
"password": "Test@123"
})
assert res.status_code == 200
token = res.json()["token"]
assert token
# 2. Call authenticated endpoint
headers = {"Authorization": f"Bearer {token}"}
me_res = requests.get(f"{BASE_URL}/auth/me", headers=headers)
assert me_res.status_code == 200
assert me_res.json()["email"] == "qa@target.dev"Curriculum Labs
Hands-On Practice Exercises
Module 1: HTTP fundamentals
Understand requests, responses, and status codes.
- 1Send a GET request and assert 200Easy 12m
- 2Read and assert a response headerEasy 12m
- 3Assert the content-type headerEasy 12m
- 4Understand 2xx status codesEasy 12m
- 5Understand 4xx status codesEasy 12m
- 6Understand 5xx status codesEasy 12m
- 7Assert the response timeMedium 25m
- 8Inspect a JSON response bodyEasy 12m
Module 2: Requests & methods
Exercise the full set of HTTP verbs.
- 1Create a resource with POSTEasy 12m
- 2Read a resource with GETEasy 12m
- 3Replace a resource with PUTMedium 25m
- 4Update a field with PATCHMedium 25m
- 5Delete a resource and verifyMedium 25m
- 6Send query parametersEasy 12m
- 7Send path parametersEasy 12m
- 8Send custom request headersMedium 25m
- 9Send a form-urlencoded bodyMedium 25m
Module 3: Working with JSON
Parse, extract, and assert on payloads.
Module 4: Schema & contracts
Validate response shape and types.
Module 5: Authentication
Test every common auth mechanism.
Module 6: Authorization & negative testing
Probe boundaries and error paths.
Module 7: Data-driven & parameterization
Cover permutations efficiently.
Module 8: GraphQL
Query and validate GraphQL APIs.
Module 9: Mocking & virtualization
Isolate services with mocks.
Module 10: Integration, performance & CI
Ship trustworthy API suites.
Want the Full 64-Challenge API Testing Curriculum?
Our comprehensive track covers HTTP fundamentals, JSON extraction, OAuth2 token rotation, rate limits, GraphQL, and downstream microservice mocking.
Frequently Asked Questions About Python (requests & pytest) Practice
Do I need to install Python, pip, or virtualenv to practice?↓
No. TargetQA's interactive playground executes Python requests syntax directly in the browser environment against live practice endpoints.
Which Python testing libraries are practiced?↓
The curriculum covers standard HTTP requests (GET, POST, PUT, PATCH, DELETE), status assertions, headers, query parameters, dict unpacking, and pytest assertion patterns.
Can I chain multiple requests in Python?↓
Yes. You can extract values from a response body or header and pass them into subsequent requests to model full end-to-end integration workflows.