這是一個使用 FastAPI 框架和 Pytest 測試框架的範例專案。專案展示了如何建立一個簡單的 API 服務,並使用 Pytest 進行單元測試和異步測試。
fastapi-pytest-example/
├── src/
│ ├── __init__.py
│ ├── __pycache__/
│ ├── main.py
│ └── utils.py
└── tests/
├── __init__.py
├── __pycache__/
├── conftest.py
├── test_main.py
└── test_utils.pysrc/main.py:定義了 FastAPI 應用的路由和處理函數。src/utils.py:包含一些輔助函數,例如長時間運行的任務和生成問候語的函數。tests/conftest.py:配置 Pytest 的一些全局設置和 fixture。tests/test_main.py:測試src/main.py中定義的 API 路由。tests/test_utils.py:測試src/utils.py中的輔助函數。
-
安裝依賴:
pdm install
-
運行 FastAPI 應用:
pdm run uvicorn src.main:app --reload
-
運行測試:
pdm run pytest
GET /health:返回服務狀態。GET /greeting:返回問候語。GET /sync:運行同步長時間任務。GET /sync-in-thread:在線程中運行同步長時間任務。GET /async:運行異步長時間任務。GET /sequential:順序運行多個異步長時間任務。GET /concurrent:並發運行多個異步長時間任務。
- :測試 API 路由的響應和行為。
- :測試輔助函數的行為和輸出。
- GGGoingdown (eddiepai0401@gmail.com)