【问题标题】:Django test not created testing database and not workDjango 测试未创建测试数据库且无法正常工作
【发布时间】:2021-12-06 19:46:16
【问题描述】:

大家

我创建了TestCase,但是当我输入命令“python manage.py test”时,这个测试不起作用。 你能帮帮我吗?

from django.test import TestCase
from accounts.models import UserProfile 



class AccountsTestCase(TestCase):    
    def setUp(self):
        print('test_setUp')
        UserProfile.objects.create(email='test@test.com', password='user111333')

    def get_user(self):    
        user = UserProfile.objects.get(email='test@test.com')

【问题讨论】:

  • 你是否在settings.py中配置了测试数据库

标签: django testing installation testcase


【解决方案1】:

您在函数开头缺少assert 方法和test_ 首字母缩写词。没有它们,您将无法进行测试。

我认为这种方法可以解决问题:

from django.test import TestCase
from accounts.models import UserProfile 

class AccountsTestCase(TestCase):    
    def setUp(self):
        print('test_setUp')
        self.someuser =UserProfile.objects.create(email='test@test.com',password='user111333')

    def test_get_user(self):    
        user = UserProfile.objects.get(email='test@test.com', password='abcd123')
        user.save()
        self.assertTrue(user)
        self.assertEqual(user.email,'test@test.com')
        self.assertNotEqual(user, self.someuser)
        self.assertNotEqual(user.email,'test@other_test.com')
 

这是一个包含更多断言的列表:

Assertions
Method Checks that
assertEqual(a, b) a == b
assertNotEqual(a, b) a != b
assertTrue(x) bool(x) is True
assertFalse(x) bool(x) is False
assertIs(a, b) a is b
assertIsNot(a, b) a is not b
assertIsNone(x) x is None
assertIsNotNone(x) x is not None
assertIn(a, b) a in b
assertNotIn(a, b) a not in b
assertIsInstance(a, b) isinstance(a, b)
        

【讨论】:

  • 欢迎您。我只是问你这个答案是否有助于将其作为答案。谢谢。
猜你喜欢
  • 2014-12-15
  • 2017-01-24
  • 2012-04-20
  • 1970-01-01
  • 1970-01-01
  • 2013-08-05
  • 2020-11-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多