Django测试未获取模型对象 [英] Django Test Not Getting Model Object
问题描述
我只是在摸索Django中的测试表面。这是我的测试代码,位于 tests.py
内:
I am just scratching the surface of testing in Django. Here is my testing code, inside tests.py
:
class AdvertisingTests( TestCase ):
def test_get_ad( self ):
'''
Test if the get ad feature is working, should always load an ad
'''
url = reverse('advertising:get_ad')
response = self.client.get( url )
self.assertEqual(response.status_code, 200)
此测试只是对应该返回广告的视图的基本测试。这是查看代码:
This test is just a basic test of a view that should return an Ad. Here is the view code:
from .models import Ad, Impression
def get_ad(request):
FirstAd = Ad.objects.first()
# +1 impression
Impression.objects.create(ad = FirstAd)
import json
data = { 'url': FirstAd.url, 'image_url': FirstAd.image.url, 'title': FirstAd.title, 'desc': FirstAd.desc }
json_data = json.dumps( data )
return HttpResponse(json_data, content_type='application/json')
I在heroku本地环境中工作,所以我运行如下测试:
heroku local:运行python manage.py测试广告
I am working in a heroku local environment, so I run the tests like this:
heroku local:run python manage.py test advertising
此测试失败,来自 Impression.objects.create(ad = FirstAd)
行:
ValueError:无法分配无: Impression.ad不允许空
值。
ValueError: Cannot assign None: "Impression.ad" does not allow null values.
告诉我的是 FirstAd
对象一片空白。好的,这样我就可以结束本地shell了: heroku local:run python manage.py shell
进行仔细检查。复制该代码没有错误:
What that tells me is that the FirstAd
object is null. OK so I wind up the local shell like this: heroku local:run python manage.py shell
to double check. Replicating that code there are no errors:
In [2]: from advertising.models import Ad, Impression
In [3]: print Ad.objects.first()
Flamingo T-Shirt Corporation
In [4]: FirstAd = Ad.objects.first()
In [5]: Impression.objects.create(ad = FirstAd)
Out[5]: <Impression: Impression object>
In [6]: exit()
所以我有点有点卡住。看来测试人员正在访问一个空的数据库。这是测试套件的正确功能吗?
So I am a little bit stuck. It seems like the tester is accessing an empty database. Is this the correct and desired functionality of the testing suite?
谢谢!
更新
好的,这一切都很正常,我应该知道这一点。我需要做的就是将setUp函数添加到测试类中以初始化数据。像这样:
OK so all this is normal, I should have known that. Adding the setUp function to my test class to initialise data is what I needed to do. Like this:
from django.core.files.uploadedfile import SimpleUploadedFile
def setUp(self):
test_user = User.objects.create_user(username='testuser', password='12345')
this_path = os.path.abspath(os.path.dirname(__file__))
banner_image = os.path.join(this_path, "static/advertising/images/pic01.jpg")
mobile_image = os.path.join(this_path, "static/advertising/images/pic02.jpg")
Ad.objects.create(
title = "Test Title",
desc = "Test Description",
image = SimpleUploadedFile(name='test_image.jpg', content=open(banner_image, 'rb').read(), content_type='image/jpeg'),
mobile_image = SimpleUploadedFile(name='test_image.jpg', content=open(mobile_image, 'rb').read(), content_type='image/jpeg'),
url = "https://www.jefferythewind.com",
user = test_user
)
推荐答案
Django te sts创建空的临时数据库,请参见文档 ,将在测试后销毁。
Django tests create empty temporaty database see docs, which will be destroyed after tests.
要使用数据预先填充测试数据库,可以使用 setUp
方法:
To prepopulate testing DB with data you can use setUp
method:
class AdvertisingTests(TestCase):
def setUp(self):
Ad.objects.create(name="myAd")
也可以使用夹具。
这篇关于Django测试未获取模型对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!