Proper POST file upload (load testing with Locust)
Well, I found the solution and I hope it will be useful for someone:
Here was described how Django handles file:How to send a "multipart/form-data" with requests in python?
And recipe is to define 'files' param in post function:
r = self.client.post("/submit/", data={ 'csrfmiddlewaretoken': csrftoken, 'password': smart_str(u'wkefjgui'), 'payload': smart_str(u'kjsdgfljdsh'), 'commit': smart_str(u'Вкрапить / Embed'), }, files={'docfile': attach})
handle multipart file
def _get_image_part(self, file_path, file_content_type='image/jpeg'): import os file_name = os.path.basename(file_path) file_content = open(file_path, 'rb') return file_name, file_content, file_content_type
multipart test case
class OpenDeviceFrontApi(TaskSet): @task(2) def rec_log_upload(self): payload = { "device_key": device_key } files = { "scene_img": self._get_image_part("data/face/rec1.jpg"), "face_img": self._get_image_part("data/face/rec2.jpg") } r = self.client.post("/log/rec_log_upload", data=payload, files=files, verify=False) assert r.status_code == 200 rData = json.loads(r.text, encoding="utf-8")