How to mock/set system date in pytest?

前端 未结 3 1607
南方客
南方客 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条回答
  •  北恋
    北恋 (楼主)
    2021-02-19 07:48

    There are two ways you can accomplish that:

    1. Create function which you will call instead of datetime.datetime.now() as suggested by Bruno, but here is different implementation:

      import os
      import datetime
      
      def mytoday():
       if 'MYDATE' in os.environ:
           return datetime.datetime.strptime(os.getenv('MYDATE'), '%m-%d-%Y').date()
       else:
           return datetime.date.today()
      

      Then, in your test, you just monkeypatch environment variable:

      import datetime
      
      def test_patched_date(monkeypatch):
          monkeytest.setenv('MYDATE', '05-31-2014')
          assert datetime.date.today() == datetime.date(2014, 5, 31)
      
    2. Monkeypatch the datetime function:

      import datetime
      import pytest
      
      FAKE_TIME = datetime.datetime(2020, 12, 25, 17, 05, 55)
      
      @pytest.fixture
      def patch_datetime_now(monkeypatch):
      
          class mydatetime:
              @classmethod
              def now(cls):
                  return FAKE_TIME
      
          monkeypatch.setattr(datetime, 'datetime', mydatetime)
      
      
      def test_patch_datetime(patch_datetime_now):
          assert datetime.datetime.now() == FAKE_TIME
      

提交回复
热议问题