有没有办法复制使用蝗虫的邮递员发生的事情

时间:2019-01-11 05:47:08

标签: python postman locust

enter image description here我正在尝试使用正文部分中的表单数据向邮递员发布请求,在该部分中,我上传了一个csv文件和一个JSON字符串,对此我得到了邮递员的回应。我想在蝗虫中复制同样的内容以进行负载测试,但由于出现错误,我无法上传文件。

  

值错误:数据不能为字符串。

下面是代码段

def project(self):
    headers = {'content-type': 'application/json', 'X-ACCESS-TOKEN': 'XXXXXXXXXXXXXXXXX'}
    files = [('params_file', ('All_params.csv', open('All_params.csv', 'rb'), 'text/csv'))]

    payload = {"df_params":{"geography":"United Arab Emirates","currency":"United Arab Emirates dirham"}}
    res = self.client.post("/api/project/setup", headers=headers,  data=json.dumps(payload), files=files, catch_response=True)
    response = res.content.decode('utf-8')
    self.response_api = json.loads(response)
    print(self.response_api)
    res.raise_for_status()

1 个答案:

答案 0 :(得分:0)

我想你可以尝试这样的事情

from locust import HttpLocust, TaskSet, task


class WebsiteTasks(TaskSet):
    @task
    def setup(self):
        headers = {'content-type': 'application/json', 'X-ACCESS-TOKEN': 'XXXXXXXXXXXXXXXXX'}
        files = [('params_file', ('All_params.csv', open('All_params.csv', 'rb'), 'text/csv'))]
        payload = {"df_params":{"geography":"United Arab Emirates","currency":"United Arab Emirates dirham"}}
        response = self.client.post("/api/project/setup", headers=headers,  data=json.dumps(payload), files=files, catch_response=True)
        response.raise_for_status()


class WebsiteUser(HttpLocust):
    task_set = WebsiteTasks
    min_wait = 5000
    max_wait = 15000

要运行,请使用$ locust -f locustfile.py

参考:https://locust.io/