Loading...
Loading...
Compare original and translation side by side
Deep Knowledge: Usewith technology:mcp__documentation__fetch_docsfor comprehensive documentation on TestClient, async testing, auth, and HTTP mocking.fastapi-testing
深度知识:使用并指定技术:mcp__documentation__fetch_docs,获取关于TestClient、异步测试、认证和HTTP模拟的完整文档。fastapi-testing
from fastapi.testclient import TestClient
from myapp.main import app
client = TestClient(app)
def test_read_root():
response = client.get("/")
assert response.status_code == 200
assert response.json() == {"message": "Hello World"}
def test_post_item():
response = client.post(
"/items/",
json={"name": "Widget", "price": 9.99},
)
assert response.status_code == 201from fastapi.testclient import TestClient
from myapp.main import app
client = TestClient(app)
def test_read_root():
response = client.get("/")
assert response.status_code == 200
assert response.json() == {"message": "Hello World"}
def test_post_item():
response = client.post(
"/items/",
json={"name": "Widget", "price": 9.99},
)
assert response.status_code == 201def test_with_lifespan():
with TestClient(app) as client: # triggers startup/shutdown
response = client.get("/items/")
assert response.status_code == 200def test_with_lifespan():
with TestClient(app) as client: # 触发启动/关闭事件
response = client.get("/items/")
assert response.status_code == 200import pytest
from httpx import ASGITransport, AsyncClient
from myapp.main import app
@pytest.mark.anyio
async def test_async():
async with AsyncClient(
transport=ASGITransport(app=app), base_url="http://test"
) as ac:
response = await ac.get("/")
assert response.status_code == 200import pytest
from httpx import ASGITransport, AsyncClient
from myapp.main import app
@pytest.mark.anyio
async def test_async():
async with AsyncClient(
transport=ASGITransport(app=app), base_url="http://test"
) as ac:
response = await ac.get("/")
assert response.status_code == 200undefinedundefinedundefinedundefinedundefinedundefinedfrom datetime import timedelta
from myapp.auth import create_access_token
def get_test_token(username="testuser") -> str:
return create_access_token(
data={"sub": username},
expires_delta=timedelta(minutes=30),
)
def test_protected_endpoint():
token = get_test_token()
response = client.get(
"/users/me/",
headers={"Authorization": f"Bearer {token}"},
)
assert response.status_code == 200
def test_no_token():
response = client.get("/users/me/")
assert response.status_code == 401from datetime import timedelta
from myapp.auth import create_access_token
def get_test_token(username="testuser") -> str:
return create_access_token(
data={"sub": username},
expires_delta=timedelta(minutes=30),
)
def test_protected_endpoint():
token = get_test_token()
response = client.get(
"/users/me/",
headers={"Authorization": f"Bearer {token}"},
)
assert response.status_code == 200
def test_no_token():
response = client.get("/users/me/")
assert response.status_code == 401def test_websocket():
with client.websocket_connect("/ws") as ws:
ws.send_text("hello")
data = ws.receive_text()
assert data == "Echo: hello"def test_websocket():
with client.websocket_connect("/ws") as ws:
ws.send_text("hello")
data = ws.receive_text()
assert data == "Echo: hello"def test_upload_file():
response = client.post(
"/uploadfile/",
files={"file": ("test.txt", b"hello world", "text/plain")},
)
assert response.status_code == 200
assert response.json() == {"filename": "test.txt", "size": 11}def test_upload_file():
response = client.post(
"/uploadfile/",
files={"file": ("test.txt", b"hello world", "text/plain")},
)
assert response.status_code == 200
assert response.json() == {"filename": "test.txt", "size": 11}from unittest.mock import patch
def test_task_called():
with patch("fastapi.BackgroundTasks.add_task") as mock:
response = client.post("/send-notification/user@example.com")
assert response.status_code == 200
mock.assert_called_once()from unittest.mock import patch
def test_task_called():
with patch("fastapi.BackgroundTasks.add_task") as mock:
response = client.post("/send-notification/user@example.com")
assert response.status_code == 200
mock.assert_called_once()import httpx
import respx
@respx.mock
def test_external_api():
respx.get("https://api.example.com/users").mock(
return_value=httpx.Response(200, json=[{"id": 1, "name": "Alice"}])
)
response = client.get("/proxy/users")
assert response.status_code == 200import httpx
import respx
@respx.mock
def test_external_api():
respx.get("https://api.example.com/users").mock(
return_value=httpx.Response(200, json=[{"id": 1, "name": "Alice"}])
)
response = client.get("/proxy/users")
assert response.status_code == 200undefinedundefinedimport responses
import requests
@responses.activate
def test_with_responses():
responses.get(
"https://api.example.com/users",
json=[{"id": 1}],
status=200,
)
result = my_service.get_users()
assert len(result) == 1import responses
import requests
@responses.activate
def test_with_responses():
responses.get(
"https://api.example.com/users",
json=[{"id": 1}],
status=200,
)
result = my_service.get_users()
assert len(result) == 1def test_real_server(httpserver):
httpserver.expect_request("/data").respond_with_json({"key": "value"})
response = requests.get(httpserver.url_for("/data"))
assert response.json() == {"key": "value"}def test_real_server(httpserver):
httpserver.expect_request("/data").respond_with_json({"key": "value"})
response = requests.get(httpserver.url_for("/data"))
assert response.json() == {"key": "value"}| Anti-Pattern | Solution |
|---|---|
| Use |
Creating | Module or session scope |
| Testing with real external APIs | Use respx/responses to mock |
Not using | Required for httpx with ASGI apps |
| 反模式 | 解决方案 |
|---|---|
未清理 | 在夹具中使用 |
每个测试创建 | 使用模块或会话作用域的 |
| 使用真实外部API进行测试 | 使用respx/responses进行模拟 |
异步测试未使用 | ASGI应用配合httpx必须使用该组件 |