0

I want to mock datetime.utcnow().isoformat() for unittesting of python3 flask application.

I already checked the StackOverflow post of Python: How do I mock datetime.utcnow()?, but couldn't adapt it to utcnow().isoformat.().

I tried to edit following code, but it didn't work.

import pytest
import unittest
from unittest import mock

def fake_datetime(*args, **kwargs):
    class FakeTime:
        @classmethod
        def utcnow(self):
            return "2020-03-17T10:02:01.285418"
    return Faketime

@mock.patch('app.utils.datetime', side_effect=fake_datetime)
class FlaskRoutesTest(unittest.TestCase):
    def setUp(self):
        self.app = app.create_app().test_client()

    def test_app_route_recovered(self, mock_datetime):
        print(mock_get.utcnow().isoformat())
Ewro
  • 447
  • 4
  • 13

1 Answers1

-1

You may use pytest-freezegun

Code sample:

@pytest.mark.freeze_time('2017-05-21')
def test_current_date():
    assert date.today() == date(2017, 5, 21)

Output

True

If you will configure the time properly, it should show you the correct ISO format time

zhanymkanov
  • 546
  • 7
  • 15
  • Thanks, but I don't want to invoke additional dependency (pytest-freezegun), if possible. – Ewro Mar 17 '20 at 11:48