0

I have this code in my class.__init__():

self.timestamp = datetime.utcnow()

I want to mock this function for testing. I want to mock 3 different dates: 03.02.2022, 02.02.2022 and 01.02.2022. So how can i do it?
I use pytest

  • 1
    Does this answer your question? [Trying to mock datetime.date.today(), but not working](https://stackoverflow.com/questions/4481954/trying-to-mock-datetime-date-today-but-not-working) – AlexisG Feb 11 '22 at 13:53
  • have a look this https://stackoverflow.com/questions/4481954/trying-to-mock-datetime-date-today-but-not-working – marti_ Feb 11 '22 at 13:54

1 Answers1

0

this library could be help you https://github.com/spulec/freezegun/

from freezegun import freeze_time
import datetime
import unittest

# Freeze time for a pytest style test:

@freeze_time("2012-01-14")
def test():
    assert datetime.datetime.now() == datetime.datetime(2012, 1, 14)
marti_
  • 130
  • 5
  • 14