api-testing

Automate FastAPI endpoint tests with pytest and httpx.

4|Updated Dec 5, 2025
One-click install
npx skills add https://github.com/aaaa47080/stock_agent --skill api-testing-aaaa47080
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: api-testing
Source: https://github.com/aaaa47080/stock_agent/tree/main/.opencode/skills/api-testing
Command: npx skills add https://github.com/aaaa47080/stock_agent --skill api-testing-aaaa47080

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

API testing patterns for FastAPI endpoints using pytest and httpx help teams validate correctness, reliability, and performance of their backend services with deterministic tests.

Core Features & Use Cases

  • Deterministic API tests using httpx.AsyncClient and pytest
  • Fixture-driven setup with TEST_MODE and mocked database for isolated unit tests
  • Structured test organization for unit and integration scenarios

Quick Start

Run the test suite with pytest and httpx against your FastAPI app, using the project's test fixtures.

Frequently Asked Questions about api-testing

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

FAQPage Schema
How do I test FastAPI endpoints with pytest and httpx?

Testing FastAPI endpoints with pytest and httpx involves using httpx.AsyncClient to send requests to your application routes. This pattern validates backend service correctness and reliability through deterministic test structures, fixtures, and isolated database setups.

What is the best way to structure unit and integration tests for a FastAPI backend?

Structuring FastAPI tests involves using pytest fixtures to manage test setups across unit and integration levels. By applying a TEST_MODE flag and mocking the database, teams achieve deterministic and isolated test execution for backend services.

Can I use httpx AsyncClient with pytest for deterministic API testing?

Yes, httpx AsyncClient works with pytest to provide deterministic API testing for FastAPI apps. By combining async request handling with pytest fixtures, backend teams can validate endpoint correctness reliably across different test scenarios without flaky behavior.

How do you mock a database for FastAPI tests using conftest.py?

Mocking a database for FastAPI tests is done by configuring pytest fixtures in conftest.py with TEST_MODE=true. This redirects database interactions to a mocked instance, ensuring isolated and deterministic unit tests that validate API patterns without touching real data.

Do I need to set TEST_MODE=true for FastAPI integration testing?

Setting TEST_MODE=true is necessary for FastAPI integration testing to activate mocked database setups. This ensures httpx AsyncClient requests are tested against an isolated environment, providing deterministic results for your backend service validations.