【发布时间】:2020-12-01 10:47:45
【问题描述】:
我正在编写一个 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
另外,我有这些测试,第一个测试创建了一个新的运营商。第二个,创建一个运营商,然后搜索数据库中所有现有的运营商。
import json
from app.infra.postgres.crud.cft import cft as pg_cft
from app.services.cft import CFTService
cft_crud = CFTService(pg_cft)
PREFIX = "/api/cft/"
def test_create_carrier(test_app_with_db):
response = test_app_with_db.post(
f"{PREFIX}",
data=json.dumps(
{
"broker_id": 1,
"carrier_id": 1,
"lower_limit": 10,
"upper_limit": 100,
"fee": 50,
"is_active": True,
}
),
)
assert response.status_code == 201
assert type(response.json()["id"]) == int
def test_get_carriers(test_app_with_db):
response = test_app_with_db.post(
f"{PREFIX}",
data=json.dumps(
{
"broker_id": 1,
"carrier_id": 1,
"lower_limit": 10,
"upper_limit": 100,
"fee": 50,
"is_active": True,
}
),
)
summary_id = response.json()["id"]
response = test_app_with_db.get(f"{PREFIX}")
assert response.status_code == 200
response_list = response.json()
assert (
len(list(filter(lambda d: d["id"] == summary_id, response_list))) == 1
)
问题是,当我使用命令 docker-compose -f Docker-compose.dev.yml exec web python -m pytest(作为 web 容器的名称)运行测试时,我收到错误消息,因为已经存在 broker_id 和 carrier_id 的组合。
我想要的是为每个测试恢复数据库。我该怎么做?
编辑:
这就是我设法做我想做的事:
import os
import pytest
from starlette.testclient import TestClient
from tortoise.contrib.test import finalizer, initializer
from app.config import Settings, get_settings
from app.main import create_application
def get_settings_override():
return Settings(testing=1, database_dev_url=os.environ.get("DATABASE_TEST_URL"))
@pytest.fixture(scope="function")
def test_app():
# set up
app = create_application()
app.dependency_overrides[get_settings] = get_settings_override
with TestClient(app) as test_client:
# testing
yield test_client
# tear down
@pytest.fixture(scope="function")
def test_app_with_db():
# set up
app = create_application()
app.dependency_overrides[get_settings] = get_settings_override
# Link with DB for testing
initializer(
["app.infra.postgres.models"],
db_url=os.environ.get("DATABASE_TEST_URL"),
)
with TestClient(app) as test_client:
# testing
yield test_client
# tear down
finalizer()
【问题讨论】:
-
您当前在模块范围的夹具中创建和销毁您的数据库。如果您想在每个测试中恢复数据库,您必须在函数范围的夹具中执行此操作(或此操作的一部分)。
-
你的
#tear down部分有什么?我目前正在自己的项目中做类似的事情,想知道你做了什么。
标签: python pytest fastapi tortoise-orm