how to unit test file upload in django

后端 未结 11 1104
感动是毒
感动是毒 2020-12-02 07:09

In my django app, I have a view which accomplishes file upload.The core snippet is like this

...
if  (request.method == \'POST\'):
    if request.FILES.has_         


        
11条回答
  •  感动是毒
    2020-12-02 07:54

    In Django 1.7 there's an issue with the TestCase wich can be resolved by using open(filepath, 'rb') but when using the test client we have no control over it. I think it's probably best to ensure file.read() returns always bytes.

    source: https://code.djangoproject.com/ticket/23912, by KevinEtienne

    Without rb option, a TypeError is raised:

    TypeError: sequence item 4: expected bytes, bytearray, or an object with the buffer interface, str found
    

提交回复
热议问题