我试图测试以下模型:
class HadoopDistributor(models.Model):
name = models.CharField(max_length=100)
def __str__(self):
return self.name
来自Django
shell的一些有效群集:
>>> from clusters.models import *
>>> hadoop_dist = HadoopDistributor.objects.all()
>>> hadoop_dist
[<HadoopDistributor: Cloudera>, <HadoopDistributor: Hortonworks>, <HadoopDistributor: MapR>, <HadoopDistributor: Apache>, <HadoopDistributor: Pivotal>]
现在我为HadoopDistribution
类编写了以下测试:
from django.test import TestCase
from clusters.models import *
# Create your tests here.
class ClusterTestCase(TestCase):
def setUp(self):
self.info = {
'hadoop_dist': 'Cloudera',
'database_repo': 'MySQL',
'datafiles': '',
'dgsecure': '4.5.2.0',
'hadoop_version': '4',
'size': '5'
}
def test_insert_cluster(self):
try:
hadoop_dist = HadoopDistributor.objects.all()
print hadoop_dist
except Exception, e:
print e
然而,当我执行python manage.py test
时,我得到了:
python manage.py test
Creating test database for alias 'default'...
[]
.
----------------------------------------------------------------------
Ran 1 test in 0.001s
OK
为什么我得到一个空数组?数据库是sqlite3
答案 0 :(得分:4)
运行测试时会创建测试数据库,因此您不会看到生产数据库中的数据。这就是查询返回空QuerySet的原因。如果希望查询返回某些内容,则需要在测试中将新数据插入数据库。