我有一个具有以下结构的函数。
def foo(*args, **kwargs):
# ... code
now = pd.Timestamp('now', tz=0)
# ... more code
return result # result depends on "now" variable
我不能在执行 foo
之前或之后获取时间戳并将其用于与返回值的比较,因为它会与函数内 now
的值不同。
答案 0 :(得分:1)
您可以使用时间模拟库,例如。 G。 https://pypi.org/project/freezegun/ 将时间冻结到测试中的已知时刻。
def test_foo():
with freezegun.freeze_time("2021-04-14"):
result = foo() # assert that result is correct for the above date