pytest-api-testing

Generate pytest tests for FastAPI REST endpoints using TestClient.

4|Updated Feb 16, 2017
One-click install
npx skills add https://github.com/nekorush14/dotfiles --skill pytest-api-testing
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: pytest-api-testing
Source: https://github.com/nekorush14/dotfiles/tree/main/configs/claude/skills/pytest-api-testing
Command: npx skills add https://github.com/nekorush14/dotfiles --skill pytest-api-testing

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill demonstrates how to write pytest-based tests for FastAPI endpoints using TestClient, including authentication and error handling scenarios.

Core Features & Use Cases

  • Request/Response Validation: Ensure endpoints return correct shapes and status codes.
  • Auth & Error Scenarios: Test protected routes and error responses.
  • Fixtures & Reusability: Use fixtures to share setup across tests.

Quick Start

Write tests using TestClient for creating and retrieving a user, and add a fixture for an in-memory database.

Frequently Asked Questions about pytest-api-testing

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I test FastAPI endpoints with pytest?

Use FastAPI's TestClient with pytest to write automated tests for endpoints. TestClient simulates HTTP requests without running a live server, letting you verify status codes, response schemas, and behavior across typical and edge-case scenarios in a single test file.

Can I test authenticated FastAPI routes with pytest?

Yes. Write pytest tests that simulate both authenticated and unauthenticated access by passing credentials or tokens through TestClient requests. Test protected routes separately to verify authorization logic and error responses for denied access.

How do I validate request and response data in FastAPI tests?

Assert HTTP status codes and inspect response JSON structure in pytest tests. Validate that endpoints return correct field names, data types, and values matching your API schema, catching mismatches between implementation and specification.

What's the best way to organize FastAPI test fixtures?

Use pytest fixtures to set up shared test data, mock databases, or TestClient instances once and reuse them across multiple tests. Fixtures reduce duplication and make tests more maintainable when testing multiple endpoints with similar setup requirements.

How do I test query parameters and pagination in FastAPI?

Pass query parameters directly in TestClient request URLs and assert that endpoints return paginated results with correct offsets, limits, and metadata. Test boundary conditions like empty pages and invalid parameter values.

Does TestClient work for testing error handling and validation failures?

Yes. TestClient captures error responses from FastAPI's built-in validation and custom error handlers. Write tests that send malformed data, missing fields, or invalid types to verify the endpoint returns appropriate status codes and error messages.