关于python:如何使用datetime对方法进行单元测试?

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

我怎样才能对它进行单元测试? 尽管这很简单,但我完全被难倒了。 这是我的第一次单元测试。


使用unittest.mock库(Python 3.3及更高版本,向后移植为mock)来替换对被测代码外部任何代码的调用。

在这里,我不仅要模拟utcnow()而且还要模拟strftime(),只返回一个字符串对象:

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()

如果您认为测试strftime()参数很重要,请将dt_mock.utcnow.return_value一个显式的datetime对象改为返回; 你必须在模拟之前创建该测试对象,因为你不能只模拟datetime.datetime.utcnow类方法:

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()

或者,在您的单元测试中,使用from datetime import datetime来保留对未被模拟的类的引用。

演示:

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'