使用Django测试时MySQL无法保存在test_数据库上

时间:2018-08-07 04:45:51

标签: python django testing

目前,我有一个Django应用程序,当在我的项目中使用他们的模型时,它可以完美地工作。但是,当我在test.py上进行测试时尝试创建新对象时,它不会保存尝试的调试,并且会返回object.pk(无)

这是我的代码:

class TestUserViews(TestCase):

    def test_authentication_success(self):

        try:
            cc = Country.objects.create(name="Lala Land")
            print('country saved')
            print(cc.id)
            print('-----')
        except Exception as error:
            print(error)

        try:
            ll = Language.objects.create(name="English")
            print('language saved')
            print(ll.id)
            print('-----')
        except Exception as error:
            print(error)

        try:
            user = User.objects.create(first_name="user name",
                                    last_name="user last",
                                    phone="000123455",
                                    identification="0000001123",
                                    address="my house",
                                    country=cc,
                                    language=ll,
                                    email="email@mail.com",
                                    password=hash_string("mypassword"))
            print('User saved')
            print(user.id)
        except Exception as error:
            print(error)

0 个答案:

没有答案