我正在为 crud 编写单元测试,我使用的框架是 FastAPI,ORM 是 tortoise,测试模块是 pytest。
我有这个配置文件:
import os
import pytest
from starlette.testclient import TestClient
from tortoise.contrib.fastapi import register_tortoise
from app.config import Settings, get_settings
from app.main import create_application
def get_settings_override():
return Settings(
testing=1, database_url=os.environ.get("DATABASE_TEST_URL")
)
@pytest.fixture(scope="module")
def test_app_with_db():
# set up
app = create_application()
app.dependency_overrides[get_settings] = get_settings_override
# Link with DB for testing
register_tortoise(
app,
db_url=os.environ.get("DATABASE_TEST_URL"),
modules={"models": ["app.infra.postgres.models"]},
generate_schemas=True,
add_exception_handlers=True,
)
with TestClient(app) as test_client:
# testing
yield test_client
# tear down
Run Code Online (Sandbox Code Playgroud)
另外,我进行了这些测试,第一个测试创建了一个新的载体。第二个,创建一个运营商,然后搜索数据库中所有现有的运营商。
import json
from app.infra.postgres.crud.cft import cft …Run Code Online (Sandbox Code Playgroud)