python - 使 pytest 使用数据库的固定日期
问题描述
我正在使用 pytest 来测试一些数学函数。我希望数据库认为这是过去的日期。
例如,我会将数据库设置为 2019 年 8 月 1 日。
有没有办法用 pytest 配置它?
目前我的数据库是这样设置的:
@pytest.fixture(autouse=True)
def autouse_db(db):
pass
解决方案
您使用什么 ORM 以及如何在查询中使用日期?如果您在查询中使用日期时间,可能的解决方案是制作简单的夹具,例如
from datetime import datetime
import pytest
@pytest.fixture(autouse=True)
def current_date():
return datetime(2019, 8, 1, 0, 0, 0)
另一种方法是在测试中使用monkeyepatch datetime.now() 函数。归根结底,这一切都源于您如何从 python 与数据库进行交互。
推荐阅读
- sql - 另一个选择查询中的 MS Access 选择查询,用于选择另一个字段值
- pyzo - 使用pyzo在python的同一目录中使用子程序时出错
- amazon-web-services - 以编程方式获取 AWS 成本和使用情况报告的内容
- jaxb - jaxb2 maven:合并两个xsds共享类型定义
- x86 - multiboot2 标头正确进入“启用了启动服务的 EFI amd64 机器状态”-hlt 指令不起作用?
- sql - HANA:如何使用动态起始值正确“创建序列开始”?
- node.js - 如何使用对象数组构建嵌套的 Mongoose Schema
- typescript - FullCalendar-vue + Typescript:属性'getApi'不存在
- redhat - 特定版本的 REDHAT linux 中的 Httpd 升级
- javascript - TypeError:无法读取未定义的属性“刺激”