如何使用日期时间对方法进行单元测试?

时间:2016-08-31 14:26:32

标签: python unit-testing 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

我如何进行单元测试?尽管这很简单,但我完全被难倒了。这是我的第一次单元测试。

1 个答案:

答案 0 :(得分:7)

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

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

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类方法:

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来保留对未被嘲笑的类的引用。

演示:

>>> 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'