【发布时间】:2014-03-03 09:11:24
【问题描述】:
我正在尝试为具有 POST 方法的类编写单元测试,用于将文档上传到基于 Web 的 django 应用程序。这是我要为其编写单元测试的视图类:
class SOP(APIView):
authentication_classes = (authentication.TokenAuthentication,)
def post(self,request):
returnDict={}
returnDict['msg']='File not uploaded'
#if form.is_valid():
newdoc = Document(sopFile = request.FILES['sopFile'])
newdoc.save()
returnDict['msg']='File uploaded'
returnDict['fileName']=newdoc.sopFile.name
# Redirect to the document list after POST
return Response(returnDict)
由于我的 django 应用程序正在使用 forms.py 来上传文件,所以我也将该代码与此一起放置:
from django import forms
class DocumentForm(forms.Form):
docfile = forms.FileField(
label='Select a file',
help_text='max. 42 megabytes'
)
我尝试使用 RequestFactory() 和 TestCase() 编写测试用例,但我不知道如何为这种类型的类/视图编写单元测试...
【问题讨论】:
标签: python django unit-testing http-post