Back to prev

Pytest 使用笔记

Jan 12, 2024
Linkang Chan
@Jesse Chan

django form post

from rest_framework.test import APITestCase
from django.test.client import MULTIPART_CONTENT, encode_multipart, BOUNDARY
from django.core.files.base import ContentFile  

class FileUploadTestCase(APITestCase):  
    def test_upload(self):
        document = ContentFile(b"foo", "test.png")
        url = reverse("upload-file")
        response = self.client.post(
            path=url,
            data=encode_multipart(data = dict(image=document, name="test"), boundary=BOUNDARY),
            content_type=MULTIPART_CONTENT,
            HTTP_AUTHORIZATION=f"Token {self.token.key}"
        )

        self.assertEqual(200, response.status_code)