FastAPI 之自動(dòng)化測(cè)試數(shù)據(jù)庫(kù)接口
今天的文章分享如下在 FastAPI 框架下,使用 pytest 來(lái)自動(dòng)化測(cè)試數(shù)據(jù)庫(kù)相關(guān)的接口,文章的最后給出全部代碼。
最近越來(lái)越喜歡使用 FastAPI 來(lái)寫(xiě)后端服務(wù)了,因?yàn)樗?Python 領(lǐng)域性能最好的 Web 框架,它專(zhuān)注于提供高性能的 Web API,其他方面并不限制你的手腳,可以隨意使用你喜歡的三方庫(kù),這點(diǎn)類(lèi)似于 Flask,可以量身定制你的后端架構(gòu),以滿(mǎn)足自己的需求。
需要說(shuō)明的是,后端服務(wù)基本是離不開(kāi)關(guān)系型數(shù)據(jù)庫(kù)的,我之前是使用 Django,Django 的 ORM 太優(yōu)秀了,以至于我從 Django 轉(zhuǎn) FastAPI 有點(diǎn)很不適應(yīng)。在 ORM 領(lǐng)域,可以說(shuō)除了 Django 的 ORM,就是 SQLAlchemy 了。所以不用 Django,就必須會(huì)用 SQLAlchemy,要快速了解,看看廖雪峰的官方網(wǎng)站的使用 SQLAlchemy[1] 來(lái)快速入門(mén)。
FastAPI 涉及數(shù)據(jù)庫(kù)的接口寫(xiě)起來(lái)并不難,跟著官方文檔sql_databases[2],5 分鐘,我們就可以生成關(guān)于數(shù)據(jù)庫(kù)的增刪改查的 Restful 風(fēng)格的 API,難的是如何自動(dòng)化的測(cè)試,
通常情況下,我們會(huì)使用 pytest 進(jìn)行自動(dòng)化單元測(cè)試,根據(jù)數(shù)據(jù)庫(kù)的記錄數(shù)來(lái)斷言,但是,每測(cè)試一次,數(shù)據(jù)庫(kù)中的記錄就保存了下來(lái),你下次測(cè)試時(shí)如果不手動(dòng)清理,那測(cè)試仍然可能失敗。
那怎么解決呢?
那就是利用數(shù)據(jù)庫(kù)的回滾功能,會(huì)改變數(shù)據(jù)庫(kù)記錄的接口測(cè)試完成后讓事務(wù)回滾,這樣每次測(cè)試完成后,數(shù)據(jù)庫(kù)的記錄數(shù)是不變的,每次運(yùn)行 pytest,數(shù)據(jù)庫(kù)的記錄數(shù)是不變的,這樣就可以進(jìn)行自動(dòng)化測(cè)試。
要想實(shí)現(xiàn)這一點(diǎn),我們需要借助于 pytest 的 fixture 功能。
pytest.fixture 是一個(gè)裝飾器,用于聲明函數(shù)是一個(gè) fixture。如果測(cè)試函數(shù)的參數(shù)列表中包含 fixture 名,那么 pytest 會(huì)檢測(cè)到,并在測(cè)試函數(shù)運(yùn)行之前執(zhí)行 fixture。
比如:
- import pytest
- @pytest.fixture()
- def some_data():
- return 42
- def test_some_data(some_data):
- assert some_data==42
fixture 包含一個(gè) scope 的可選參數(shù),用于控制 fixture 執(zhí)行配置和銷(xiāo)毀邏輯的頻率:
- scope='function' 函數(shù)級(jí)別的 fixture 每個(gè)測(cè)試函數(shù)只運(yùn)行一次。配置代碼在測(cè)試用例運(yùn)行之前運(yùn)行,銷(xiāo)毀代碼在測(cè)試用例運(yùn)行之后執(zhí)行。function 是 fixture 參數(shù)的默認(rèn)值。
- scope='class' 類(lèi)級(jí)別的 fixture 每個(gè)測(cè)試類(lèi)只運(yùn)行一次,不管測(cè)試類(lèi)中有多少個(gè)類(lèi)方法都可以共享這個(gè) fixture
- scope='module' 模塊級(jí)別的 fixture 每個(gè)模塊只運(yùn)行一次,不管模塊里有多少個(gè)測(cè)試函數(shù),類(lèi)方法或其他 fixture 都可以共享這個(gè)fixture
- scope='session' 會(huì)話(huà)級(jí)別的 fixture 每次會(huì)話(huà)只運(yùn)行一次。一次 pytest 會(huì)話(huà)中的所有測(cè)試函數(shù)、方法都可以共享這個(gè) fixture
比如說(shuō)讓數(shù)據(jù)庫(kù)回滾的,我們就可以寫(xiě)一個(gè)這樣的 fixture:
- @pytest.fixture(scope="function")
- def db(db_engine):
- connection = db_engine.connect()
- # begin a non-ORM transaction
- connection.begin()
- # bind an individual Session to the connection
- db = Session(bind=connection)
- # db = Session(db_engine)
- app.dependency_overrides[get_db] = lambda: db
- yield db
- db.rollback()
- connection.close()
當(dāng)然還有很多 fixture,比如說(shuō)創(chuàng)建數(shù)據(jù)庫(kù)引擎:
- @pytest.fixture(scope="session")
- def db_engine():
- engine = create_engine(SQLALCHEMY_DATABASE_URL)
- if not database_exists:
- create_database(engine.url)
- Base.metadata.create_all(bind=engine)
- yield engine
再比如,在測(cè)試前,數(shù)據(jù)庫(kù)中先插入 2 條數(shù)據(jù):
- @pytest.fixture
- def items(db):
- create_item(db, schemas.ItemCreate(title="item 1"))
- create_item(db, schemas.ItemCreate(title="item 2"))
把這些 fixture 函數(shù)放在文件名conftest.py 中,pytest 會(huì)自動(dòng)讀取并執(zhí)行。至于為什么放在 conftest.py中,請(qǐng)查閱 pytest 文檔,這里不展開(kāi),
接下來(lái),利用這些 fixture,編寫(xiě)單元測(cè)試用例,一個(gè)示例如下:
- from fastapi.testclient import TestClient
- from . import crud
- from .main import app
- def test_post_items(db):
- client = TestClient(app)
- client.post("/items/", json={"title": "Item 1"})
- client.post("/items/", json={"title": "Item 2"})
- client.post("/items/", json={"title": "Item 3"})
- items = crud.get_items(db)
- assert len(items) == 3
- def test_list_items(items, client):
- response = client.get("/items")
- assert len(response.json()) == 2
其中 test_post_items,測(cè)試的是提交了 3 個(gè)數(shù)據(jù),然后斷言數(shù)據(jù)庫(kù)中的記錄數(shù)為 3。test_list_items 有個(gè)參數(shù)是 items,會(huì)調(diào)用之前的 fixture,提前往數(shù)據(jù)庫(kù)插入了 2 條記錄,因此斷言記錄數(shù)為 2。
每個(gè)測(cè)試函數(shù)執(zhí)行時(shí)互不影響,執(zhí)行完成后,數(shù)據(jù)庫(kù)都會(huì)回滾,測(cè)試前 items 是空的,測(cè)試之后 表仍然是空的,這樣就可以自動(dòng)進(jìn)行數(shù)據(jù)庫(kù)的測(cè)試了。
完整代碼
不能選擇 sqlite 數(shù)據(jù)庫(kù)進(jìn)行測(cè)試,因?yàn)樗恢С植l(fā)訪問(wèn)。
代碼的數(shù)據(jù)庫(kù)配置為 mysql,用戶(hù)名、密碼、數(shù)據(jù)庫(kù)名請(qǐng)自行修改后執(zhí)行。