Skip to content

feat: add comprehensive custom API tests #13938

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
🎨 [pre-commit.ci] Auto format from pre-commit.com hooks
  • Loading branch information
pre-commit-ci[bot] committed Jul 27, 2025
commit e6164a272bb2918f15ebc7d69d216ff81dc4e6ef
48 changes: 22 additions & 26 deletions tests/test_custom_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
自定义API测试
这是一个示例测试文件,展示如何在FastAPI项目中编写测试
"""

from typing import Optional

import pytest
from fastapi import FastAPI
from fastapi.testclient import TestClient
from pydantic import BaseModel
from typing import Optional


# 创建一个简单的FastAPI应用用于测试
app = FastAPI()
Expand Down Expand Up @@ -53,68 +54,67 @@ def get_user(user_id: int):
# 测试用例
class TestCustomAPI:
"""自定义API测试类"""

def test_read_root(self):
"""测试根路径"""
response = client.get("/")
assert response.status_code == 200
assert response.json() == {"message": "Hello Custom API"}

def test_read_item_with_params(self):
"""测试带参数的商品查询"""
response = client.get("/items/5?q=somequery")
assert response.status_code == 200
assert response.json() == {"item_id": 5, "q": "somequery"}

def test_read_item_without_params(self):
"""测试不带查询参数的商品查询"""
response = client.get("/items/10")
assert response.status_code == 200
assert response.json() == {"item_id": 10, "q": None}

def test_create_item(self):
"""测试创建商品"""
item_data = {
"name": "Test Item",
"price": 99.99,
"is_offer": True
}
item_data = {"name": "Test Item", "price": 99.99, "is_offer": True}
response = client.post("/items/", json=item_data)
assert response.status_code == 200
response_data = response.json()
assert response_data["status"] == "created"
assert response_data["item"]["name"] == "Test Item"
assert response_data["item"]["price"] == 99.99
assert response_data["item"]["is_offer"] is True

def test_get_user(self):
"""测试获取用户信息"""
response = client.get("/users/123")
assert response.status_code == 200
assert response.json() == {"user_id": 123, "username": "user_123"}

@pytest.mark.parametrize("item_id,expected_id", [
(1, 1),
(42, 42),
(999, 999),
])

@pytest.mark.parametrize(
"item_id,expected_id",
[
(1, 1),
(42, 42),
(999, 999),
],
)
def test_read_item_parametrized(self, item_id, expected_id):
"""参数化测试:测试不同的商品ID"""
response = client.get(f"/items/{item_id}")
assert response.status_code == 200
assert response.json()["item_id"] == expected_id

def test_invalid_item_id(self):
"""测试无效的商品ID"""
response = client.get("/items/not_a_number")
assert response.status_code == 422 # 验证错误

def test_create_item_invalid_data(self):
"""测试创建商品时的无效数据"""
invalid_data = {
"name": "Test Item",
# 缺少必需的price字段
"is_offer": True
"is_offer": True,
}
response = client.post("/items/", json=invalid_data)
assert response.status_code == 422 # 验证错误
Expand All @@ -139,11 +139,7 @@ def test_openapi_schema():
@pytest.fixture
def sample_item():
"""测试用的示例商品数据"""
return {
"name": "Sample Item",
"price": 50.0,
"is_offer": False
}
return {"name": "Sample Item", "price": 50.0, "is_offer": False}


def test_with_fixture(sample_item):
Expand Down
Loading