我正在尝试为具有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()编写测试用例,但是我无法弄清楚如何为这种类型的类/视图编写单元测试...
您可以使用Django的测试客户端。它很容易使用。
Django文档中的示例:
>>> c = Client()
>>> with open('wishlist.doc') as fp:
... c.post('/customers/wishes/', {'name': 'fred', 'attachment': fp})
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句