0

我有以下内容tests.py

class TestSugar(TestCase):
    def test_empty_sugar_applicants(self):
        Sugar_Applicant.objects.using('sugarcrm').all().delete();
        number_of_sugar_applicants = Sugar_Applicant.objects.using('sugarcrm').filter(deleted=0).count()
        self.assertEqual(number_of_sugar_applicants, 0)

应该像在我的代码的其他区域一样,调用sugarcrm 数据库并访问相关表。但是,由于某种原因,在 tests.py 文件中,test_会在名称之前插入:

Traceback (most recent call last):
  File "C:\Users\jont\Documents\application_trackingwc\atp\jobs\tests.py", line 27, in test_empty_sugar_applicants
    number_of_sugar_applicants = Sugar_Applicant.objects.using('sugarcrm').filter(deleted=0).count()
  File "C:\Python27\lib\site-packages\django\db\models\query.py", line 338, in count
    return self.query.get_count(using=self.db)
  File "C:\Python27\lib\site-packages\django\db\models\sql\query.py", line 424, in get_count
    number = obj.get_aggregation(using=using)[None]
  File "C:\Python27\lib\site-packages\django\db\models\sql\query.py", line 390, in get_aggregation
    result = query.get_compiler(using).execute_sql(SINGLE)
  File "C:\Python27\lib\site-packages\django\db\models\sql\compiler.py", line 786, in execute_sql
    cursor.execute(sql, params)
  File "C:\Python27\lib\site-packages\django\db\backends\utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "C:\Python27\lib\site-packages\django\db\utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "C:\Python27\lib\site-packages\django\db\backends\utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "C:\Python27\lib\site-packages\django\db\backends\mysql\base.py", line 128, in execute
    return self.cursor.execute(query, args)
  File "C:\Python27\lib\site-packages\MySQLdb\cursors.py", line 205, in execute
    self.errorhandler(self, exc, value)
  File "C:\Python27\lib\site-packages\MySQLdb\connections.py", line 36, in defaulterrorhandler
    raise errorclass, errorvalue
ProgrammingError: (1146, "Table 'test_sugarcrm.a1234_applicants' doesn't exist")

模型.py

class Sugar_Applicant(models.Model):
    id = models.CharField(max_length=36, blank=False, primary_key=True)
    name = models.CharField(max_length=100, blank=True)
    applicant_title = models.CharField(max_length=100, blank=True)
    local_id = models.CharField(max_length=255, blank=True)
    other_title = models.CharField(max_length=255, blank=True)
    dob = models.CharField(max_length=255, blank=True)
    location = models.CharField(max_length=255, blank=True)
    nationality = models.CharField(max_length=255, blank=True)
    other_nationality = models.CharField(max_length=255, blank=True)
    national_insurance_number = models.CharField(max_length=255, blank=True)
    primary_address_street = models.CharField(max_length=150, blank=True)
    primary_address_street_2 = models.CharField(max_length=150, blank=True)
    primary_address_district = models.CharField(max_length=255, blank=True)
    primary_address_city = models.CharField(max_length=100, blank=True)
    primary_address_county = models.CharField(max_length=255, blank=True)
    primary_address_postcode = models.CharField(max_length=20, blank=True)
    telephone = models.CharField(max_length=100, blank=True)
    mobile_phone = models.CharField(max_length=100, blank=True)
    date_modified = models.DateTimeField(default=timezone.now)
    deleted = models.IntegerField(blank=True)
    approved = models.CharField(max_length=255, blank=True)
    job = models.CharField(max_length=50, blank=True)
    location = models.CharField(max_length=50, blank=True)

    class Meta:
        managed = False
        db_table = 'a1234_applicants'

如果我把这段代码放在外面def,它工作正常,

class TestSugar(TestCase):
    Sugar_Applicant.objects.using('sugarcrm').all().delete();
    number_of_sugar_applicants = Sugar_Applicant.objects.using('sugarcrm').filter(deleted=0).count()
    def test_empty_sugar_applicants(self):
        self.assertEqual(number_of_sugar_applicants, 0)
4

1 回答 1

0

测试套件做了几件事来阻止您更改生产数据……这是一件非常好的事情。

如果您想编写使用数据的测试,您将需要创建一个夹具。除其他外,拥有固定的测试数据将使您知道您运行的每个测试的正确结果是什么。

更多信息在这里:http ://django-testing-docs.readthedocs.org/en/latest/fixtures.html

于 2015-01-22T16:28:05.137 回答