首页 > 解决方案 > 使用 pytest 进行测试时如何使用夹具正确清除数据库

问题描述

我有一个测试数据库。假设在那里创建了三个表:

create table users (
    id serial constraint users_pkey primary key,
    name text not null
);
create table roles (
    id serial constraint roles_pkey primary key,
    name text not null
);
create table users_roles (
    user_id int constraint users_roles_users_fkey references users(id),
    role_id int constraint users_roles_roles_fkey references roles(id)
);

每个测试都从使用 factory_boy 工厂向数据库填充数据开始。典型的测试如下所示:

def test_get_user_roles(api_client):
    user = UserFactory.create(id=1)
    role = RolesFactory.create(id=1)
    users_roles = UsersRolesFactory.create(user_id=1, role_id=1)
    response = api_client.get(f"/users/{user.id}/roles")
    assert ...

我可以使用 @pytest.mark.usefixtures("clear_users_roles", "clear_users", "clear_roles") 清除表,其中 "clear_users_roles"、"clear_users"、"clear_roles" 是如果表之间没有关系则明显清除表的固定装置. 但是在上面的示例中,表 users_roles 和用户之间以及 users_roles 和角色之间存在关系(外键),问题是固定装置无序运行。所以当我运行我的测试时,我得到一个完整性错误,因为,例如,夹具“clear_users”在“clear_users_roles”之前执行,我的 RDBMS 无法删除记录,因为该记录仍然引用表“users”。有没有按特定顺序运行固定装置的正确方法?或者对于这样的案例可能有一些模式/最佳实践?

标签: pythonpython-3.xtestingpytestfactory-boy

解决方案


您是否尝试过在带有 yield 关键字的夹具中使用拆卸代码?

来自pytest 文档的示例:

# content of conftest.py

import smtplib
import pytest


@pytest.fixture(scope="module")
def smtp_connection():
    smtp_connection = smtplib.SMTP("smtp.gmail.com", 587, timeout=5)
    yield smtp_connection  # provide the fixture value
    print("teardown smtp")
    smtp_connection.close()

推荐阅读