使用 pytest 进行测试时如何使用装置正确清除数据库

Iva*_*ich 5 python testing pytest python-3.x factory-boy

我有一个测试数据库。假设其中创建了三个表:

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)
);
Run Code Online (Sandbox Code Playgroud)

每个测试都从使用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 ...
Run Code Online (Sandbox Code Playgroud)

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

Ton*_*rmi 2

您是否尝试过在您的装置中使用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()
Run Code Online (Sandbox Code Playgroud)