0

我试图datetime.now用包模拟两个连续的调用freezegun,但无法弄清楚为每个调用设置不同的返回值的方法。

from datetime import datetime, timedelta

from freezegun import freezetime


def function_to_test():
    start = datetime.now()
    # do something
    end = datetime.now()
    return start, end

def test():
    datetimes = (d for d in [datetime(2000, 5, 12, 4, 20), datetime(2000, 5, 12, 4, 20) + timedelta(seconds=50)])
    with freezetime(datetimes):
        start, end = function_to_test()
    assert start == datetime(2000, 5, 12, 4, 20)
    # this will fail because I'd have to call freezetime once again to have time set to new value
    assert end == (datetime(2000, 5, 12, 4, 20) + timedelta(seconds=50))
    

有没有办法获得与 forunittest.mock.Mock和设置类似side_effect的结果iterable

4

0 回答 0