Skip to content Skip to sidebar Skip to footer

Patching Datetime.timedelta.total_seconds

I write unit-tests for web application and i should change function waiting time TIME_TO_WAIT to test some modules. Example of code: import time from datetime import datetime as d

Solution 1:

As I wrote in the comment - I would patch out dt and time in order to control the speed of of test execution like so:

from unittest import TestCase
from mock import patch
from datetime import datetime

from tested.module import function_under_test

class FunctionTester(TestCase):

    @patch('tested.module.time')
    @patch('tested.module.dt')
    def test_info_query(self, datetime_mock, time_mock):
        datetime_mock.now.side_effect = [
            datetime(year=2000, month=1, day=1, hour=0, minute=0, second=0),
            datetime(year=2000, month=1, day=1, hour=0, minute=5, second=0),
            # this should be over the threshold
            datetime(year=2000, month=1, day=1, hour=0, minute=5, second=1),
        ]
        value = function_under_test()
        # self.assertEquals(value, ??)
        self.assertEqual(datetime_mock.now.call_count, 3)

Post a Comment for "Patching Datetime.timedelta.total_seconds"