测试我的功能时间转换器的问题

时间:2018-07-31 10:32:39

标签: python-3.x datetime testing python-unittest

我正在尝试测试我的功能,但对我而言,测试表现出了意外的方式

def time_converter(time_zone=None, date_time=None):
    if not time_zone and not date_time:
        return None
    else:
        from_dt = datetime.strptime(date_time, '%Y-%m-%dT%H:%M:%S.%f')
        time_zone = pytz.timezone(time_zone)
        from_dt = from_dt.astimezone(time_zone)
        from_dt = from_dt.strftime('%Y-%m-%dT%H:%M:%S.%f%z')
        date_time = from_dt[:23]
        time_zone = from_dt[-5:]
        return date_time + time_zone

这是我的测试

class TestTimeConverter(unittest.TestCase):

    def test_none_datetime_timezone_converter(self):
        result = time_converter()
        self.assertIsNone(result)

    def test_ok_datetime_timezone_converter(self):
        time_zone = 'Asia/Bishkek'
        test = '2018-07-26T06:00:00.001+0600'
        input = '2018-07-26T06:00:00.001'
        result = time_converter(time_zone, input)
        self.assertEqual(result, test)

如果我在本地计算机上运行测试,一切都很好 但是当我将代码推送到gitlab上时 这是问题

def test_ok_datetime_timezone_converter(self):
    time_zone = 'Asia/Bishkek'
    test = '2018-07-26T06:00:00.001+0600'
    input = '2018-07-26T06:00:00.001'
    result = time_converter(time_zone, input)
  self.assertEqual(result, test)
     

E AssertionError:'2018-07-26T12:00:00.001 + 0600'!='2018-07-26T06:00:00.001 + 0600'   电子邮件-2018-07-26T12:00:00.001 + 0600   E? ^^   电子邮件+ 2018-07-26T06:00:00.001 + 0600   E? ^^

 tests/test_time_converter.py:16: AssertionError
 ====================== 1 failed, 1 passed in 0.07 seconds ======================
 ERROR: Job failed: exit code 1

我试图模拟此类以返回所需的值,但是            E TypeError:无法设置内置/扩展类型的属性           'datetime.datetime'

0 个答案:

没有答案