Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django Pytest图像上载-错误rest_framework.exceptions.ParseError:多部分表单解析错误-多部分中的边界无效_Django_Django Rest Framework_Pytest_Pytest Django - Fatal编程技术网

Django Pytest图像上载-错误rest_framework.exceptions.ParseError:多部分表单解析错误-多部分中的边界无效

Django Pytest图像上载-错误rest_framework.exceptions.ParseError:多部分表单解析错误-多部分中的边界无效,django,django-rest-framework,pytest,pytest-django,Django,Django Rest Framework,Pytest,Pytest Django,我正在测试一个将图像上传到用户配置文件的API调用。下面的测试用例返回状态代码400。如果我从数据中删除image_filename键,测试用例将成功。如何在pytest中测试图像上载 def test_edit_user_profile(db, client): # stream the image into binary with open('C:/...../test.png', 'rb') as consultant_image:

我正在测试一个将图像上传到用户配置文件的API调用。下面的测试用例返回状态代码400。如果我从数据中删除image_filename键,测试用例将成功。如何在pytest中测试图像上载

def test_edit_user_profile(db, client):    
         # stream the image into binary
         with open('C:/...../test.png', 'rb') as consultant_image:
                 image_binary = BytesIO(consultant_image.read())

         userdetail_response = client.patch(path=reverse('user-detail', args="1"),
                                        data={"full_name": "James edited",
                                              "image_filename": (image_binary, 'test2.png')},
                                        content_type='multipart/form-data',
                                        HTTP_AUTHORIZATION='Token ' + data.json()['token_key'])

         assert userdetail_response.status_code == status.HTTP_200_OK
这个解决方案奏效了

from django.test.client import BOUNDARY, MULTIPART_CONTENT, encode_multipart

    userdetail_response2 = client.patch(
        path=reverse('user-detail', args=(str(registered_response.json()['id']),)),
        data=encode_multipart(BOUNDARY,
                            {
                                "full_name": "James",
                                "bio": "Details about me",
                                "is_allow_direct_order": False,
                                'image_filename': open((os.path.dirname(os.path.abspath(__file__))) + '/test.png', 'rb'),
                            }),
        content_type=MULTIPART_CONTENT,
        HTTP_AUTHORIZATION='Token ' + registered_response.json()['token_key']
    )