- 1. FastAPI 입문: 설치부터 Hello World 까지
- 2. Path 와 Query 매개변수 (URL 로 데이터 받기)
- 3. Pydantic 으로 Request Body 다루기
- 4. Response Model 과 HTTP 상태 코드
- 5. 예외 처리 (HTTPException 완벽 가이드)
- 6. 의존성 주입 (Dependency Injection) 정복
- 7. SQLAlchemy 비동기 데이터베이스
- 8. JWT 인증과 보안 (로그인부터 토큰 갱신까지)
- 9. 실시간 통신 (WebSockets & SSE)
- 10. 테스트 작성법 (Pytest 완전 가이드)
10. 테스트 작성법 (Pytest 완전 가이드)
# 10 강. 테스트 작성법 (Pytest)
## 🎯 학습 목표
- Pytest 기본 사용법
- FastAPI TestClient
- 의존성 오버라이드
---
## 1. 첫 테스트
```python
# tests/test_basic.py
def test_addition():
assert 1 + 1 == 2
```
실행:
```bash
pytest
```
---
## 2. FastAPI 테스트
```python
from fastapi.testclient import TestClient
from main import app
client = TestClient(app)
def test_read_root():
response = client.get("/")
assert response.status_code == 200
assert response.json() == {"message": "Hello World"}
```
---
## 3. 의존성 오버라이드
```python
@app.get("/users/me")
def read_me(current_user = Depends(get_current_user)):
return current_user
# 테스트에서
async def override_get_current_user():
return {"id": 1, "email": "test@example.com"}
app.dependency_overrides[get_current_user] = override_get_current_user
```