Python 使pytest为数据库使用固定日期
我正在使用pytest测试一些数学函数。我希望数据库认为这是一个过去的日期 例如,我想将数据库设置为2019年8月1日 有没有办法用pytest来配置它 当前我的数据库设置如下:Python 使pytest为数据库使用固定日期,python,pytest,Python,Pytest,我正在使用pytest测试一些数学函数。我希望数据库认为这是一个过去的日期 例如,我想将数据库设置为2019年8月1日 有没有办法用pytest来配置它 当前我的数据库设置如下: @pytest.fixture(autouse=True) def autouse_db(db): pass 您在查询中使用什么ORM以及如何使用日期? 如果在查询中使用datetime,可能的解决方案是使简单的fixture像 从日期时间导入日期时间 导入pytest @pytest.fixture(aut
@pytest.fixture(autouse=True)
def autouse_db(db):
pass
您在查询中使用什么ORM以及如何使用日期? 如果在查询中使用datetime,可能的解决方案是使简单的fixture像
从日期时间导入日期时间
导入pytest
@pytest.fixture(autouse=True)
定义当前日期():
返回日期时间(2019、8、1、0、0、0)
另一种方法是在测试中使用monkeyepatch datetime.now()函数。
归根结底,这一切都是从python如何与数据库交互开始的。用于monkeypatchingdatetime
函数。例如:
import datetime
import pytest
from freezegun import freeze_time
@pytest.fixture
def fast_forward_to_2038():
# use me to test the year 2038 problem
with freeze_time('2038-01-19 03:14:08'):
# year 2038 problem should be reproducible now
yield
@pytest.mark.usefixtures('fast_forward_to_2038')
def test_2038_problem():
assert datetime.datetime.now() == datetime.datetime(2038, 1, 19, 03, 14, 08)
...
查看软件包中的更多示例。使用sql alchemy作为ORM,日期的设置如下:start_date=datetime.datetime.strtime('2016-04-23',%Y-%m-%d')我最后使用了带有冰枪的装饰器,如下所示:@freeze_time(“2018-08-01”)