python - 使用 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”。有没有按特定顺序运行固定装置的正确方法?或者对于这样的案例可能有一些模式/最佳实践?
解决方案
您是否尝试过在带有 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()
推荐阅读
- javascript - BingMaps 是否可以在特定缩放级别以下禁用集群?
- r - 使用 Tidyverse 计算 REDCap 数据中特定字符串在多列中出现的频率
- swift - 如何从 Locale 获取本地化代码?
- kotlin - Kotlin 中出现意外标记(使用 ';' 分隔同一行上的表达式)错误
- css - 如何在 React JS 中使用 Google Fonts API?
- javascript - YouTube Data API Insert 方法缺少什么参数
- c++ - 找不到 C++ CodeBlocks 和 wxWidgets 文件?
- log4j - log4j2.8.2.jar 是否适用于 JDK 1.7。如何找到 jar 文件支持的 jdk 版本?
- deep-learning - 我可以对以下图像使用哪些异常检测算法(监督或非监督)?
- python - 气流中的 ImportError:没有名为 acme.operators.dwh_operators 的模块