How to mock/set system date in pytest?

前端 未结 3 1630
南方客
南方客 2021-02-19 07:13

In some of my tests I am having a problem that they fail on Travis because of time and time zone problems, so I want to mock system time for my test. How can I do this?

3条回答
  •  Happy的楠姐
    2021-02-19 07:26

    @Brian-Kruger's answer is the best one. I've voted to undelete it. In the meantime...

    Use freezegun (repo).

    From the README:

    from freezegun import freeze_time
    
    @freeze_time("2012-01-14")
    def test():
        assert datetime.datetime.now() == datetime.datetime(2012, 1, 14)
    

提交回复
热议问题