How can I unit test a method using a datetime?
我有以下类和方法:
1 2 3 4 5 6 | class DateTimeHelper(object): @staticmethod def get_utc_millisecond_timestamp(): (dt, micro) = datetime.datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f').split('.') return"%s.%03d" % (dt, int(micro) / 1000) # UTC time with millisecond |
我怎样才能对它进行单元测试? 尽管这很简单,但我完全被难倒了。 这是我的第一次单元测试。
使用
在这里,我不仅要模拟
1 2 3 | with mock.patch('datetime.datetime') as dt_mock: dt_mock.utcnow.return_value.strftime.return_value = '2016-08-04 12:22:44.123456' result = DateTimeHelper.get_utc_millisecond_timestamp() |
如果您认为测试
1 2 3 4 | testdt = datetime.datetime(2016, 8, 4, 12, 22, 44, 123456) with mock.patch('datetime.datetime') as dt_mock: dt_mock.utcnow.return_value = testdt result = DateTimeHelper.get_utc_millisecond_timestamp() |
或者,在您的单元测试中,使用
演示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | >>> from unittest import mock >>> import datetime >>> class DateTimeHelper(object): ... @staticmethod ... def get_utc_millisecond_timestamp(): ... (dt, micro) = datetime.datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f').split('.') ... return"%s.%03d" % (dt, int(micro) / 1000) # UTC time with millisecond ... >>> with mock.patch('datetime.datetime') as dt_mock: ... dt_mock.utcnow.return_value.strftime.return_value = '2016-08-04 12:22:44.123456' ... result = DateTimeHelper.get_utc_millisecond_timestamp() ... >>> result '2016-08-04 12:22:44.123' >>> testdt = datetime.datetime(2016, 8, 4, 12, 22, 44, 123456) >>> with mock.patch('datetime.datetime') as dt_mock: ... dt_mock.utcnow.return_value = testdt ... result = DateTimeHelper.get_utc_millisecond_timestamp() ... >>> result '2016-08-04 12:22:44.123' |