AssertionError:datetime.datetime(2017,9,20,4,43,24,206775,tzinfo = <utc>)!='2017-09-19 08:47:51.807533'

时间:2017-09-20 05:03:47

标签: python django datetime

我运行tests.py进行测试,所以我收到错误

self.assertEqual(self.user1.regist_date, '2017-09-19 08:47:51.807533')
AssertionError: datetime.datetime(2017, 9, 20, 4, 43, 24, 206775, tzinfo=<UTC>) != '2017-09-19 08:47:51.807533' 

我在tests.py中写过

#coding:utf-8
from django.test import TestCase
from app.models import User

# Create your tests here.
class UserModelTests(TestCase):
    def setUp(self):
        self.user1 = User.objects.create(name='Tom', regist_date = '2017-09-19 08:47:51.807533',age=20)

    def test_user1_name(self):
        self.assertEqual(self.user1.name, 'Tom')

    def test_user1_regist_date(self):
        self.assertEqual(self.user1.regist_date, '2017-09-19 08:47:51.807533')

    def test_user1_age(self):
        self.assertEqual(self.user1.age, 20)

models.py是

class User(models.Model):
    name = models.CharField(max_length=200,null=True)
    regist_date = models.DateTimeField(auto_now=True)
    age = models.IntegerField(max_length=10,null=True)

我认为regist_date的类型是datetime.datetime而不是字符串,所以我重写了 test.py的setUp是self.user1 = User.objects.create(name='Tom', regist_date = datetime.datetime(2017, 9, 20, 4, 43, 24, 206775, tzinfo=<UTC>),age=20),所以无法分配给函数调用错误.y方法出了什么问题?如何解决这个问题?

追溯是

Traceback (most recent call last):
  File "/Users/uu121291/recomin/app/tests.py", line 17, in test_user1_regist_date
    self.assertEqual(self.user1.regist_date, datetime.strptime('2017-09-20 06:03:48.570498', '%Y-%m-%d %H:%M:%S.%f'))
AssertionError: datetime.datetime(2017, 9, 20, 6, 7, 49, 51089, tzinfo=<UTC>) != datetime.datetime(2017, 9, 20, 6, 3, 48, 570498)

----------------------------------------------------------------------
Ran 18 tests in 0.076s

FAILED (failures=1)
Destroying test database for alias 'default'...

0 个答案:

没有答案