Pure Windows는 적합하지 않을 것 같아 WSL에서 Docker 기반으로 환경을 구성하고
Docker Desktop 설치후
WSL 환경에서 Docker image 로 my-fastapi-app 를 준비함
" docker build -t my-fastapi-app . "
WSL 환경에서 Docker container 로 fastapi-app 로 데몬으로 준비, 포트 8000 연결조건
docker run -d -p 8000:8000 --name fastapi-app my-fastapi-app
WSL 환경에서 Docker containter를 종료시 삭제조건과 app폴더를 연결하는 조건
docker run -d -p 8000:8000 --rm -v $(pwd):/app my-fastapi-app
WSL 환경에서 Docker containter에게 이름 fastapi-app 지정하는 조건
docker run -d -p 8000:8000 -v $(pwd):/app --name fastapi-app my-fastapi-app
WSL 에서 Docker container fastapi-app 에 연결하기
docker exec -it fastapi-app /bin/bash
# 1. 경량 Python 베이스 이미지 사용
FROM python:3.11-slim
# 2. 환경 변수 설정 (버퍼링 비활성화, UTF-8 지원)
ENV PYTHONUNBUFFERED=1 \
PYTHONDONTWRITEBYTECODE=1
# 3. 작업 디렉터리 생성 및 설정
WORKDIR /app
# 4. 의존성 설치를 위한 requirements.txt 복사
COPY requirements.txt .
# 5. 패키지 설치 (캐시 최소화 및 빌드 최적화)
RUN pip install --no-cache-dir --upgrade pip \
&& pip install --no-cache-dir -r requirements.txt
# 6. 애플리케이션 코드 복사
COPY . .
# 7. FastAPI 서버 실행
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000", "--reload"]
fastapiuvicornhttpierequestshttpxblackpytest
FastAPI hello world
from fastapi import FastAPI
app = FastAPI()
@app.get("/hi")
def say_hello():
return {"message": "Hello, World"}
연습코드
https://github.com/johlim/FastAPIAWS 에서 운용하는 EC2 들은 최근 python version을 지원하지 않는 경우가 많다.
경험적으로는 3.8이 기본이고 3.9까지는 수동 설치가 가능하므로
python 3.9 까지 지원하는 문법을 사용하는것이 호환성을 개선할수 있다.
자동테스트
schemathesis run http://localhost:8000/openapi.json --experimental=openapi-3.1
결과
---
Performed checks:
not_a_server_error 1408 / 1420 passed FAILED
WARNINGS:
- Most of the responses from `GET /user/token` have a 401 status code. Did you specify proper API credentials?
- Most of the responses from `GET /auth/who` have a 401 status code. Did you specify proper API credentials?
- `POST /explorer` returned only 4xx responses during unit tests. Check base URL or adjust data generation settings
- `GET /explorer/{name}/` returned only 4xx responses during unit tests. Check base URL or adjust data generation settings
- `DELETE /explorer/{name}/` returned only 4xx responses during unit tests. Check base URL or adjust data generation settings
- `GET /explorer/{name}` returned only 4xx responses during unit tests. Check base URL or adjust data generation settings
- `DELETE /explorer/{name}` returned only 4xx responses during unit tests. Check base URL or adjust data generation settings
Experimental Features:
- OpenAPI 3.1: Support for response validation
Feedback: https://github.com/schemathesis/schemathesis/discussions/1822
Your feedback is crucial for experimental features. Please visit the provided URL(s) to share your thoughts.
Note: Use the 'X-Schemathesis-TestCaseId' header to correlate test case ids from failure messages with server logs for debugging.
Note: To replicate these test failures, rerun with `--hypothesis-seed=202434289134074871147673993360901731981`
======================================================================== 22 passed, 6 failed in 73.65s =========================================================================
--
댓글