DRF- Post Request Unitest



我在View Set下有一个post方法。我需要为这个方法写一个单元测试用例。当我传递参数时,它给出None。我应该如何传递参数和数据(有效载荷)。

views.py
@action(detail=True, methods=['post'])
def complete_task(self, request, *args, **kwargs):
"""
Method for complete the task
input post request : task_id : str, variable_return:boolean, request data: dict
output Response : gives whether task is completed or not
"""
try:
get_task_id = self.request.query_params.get("task_id")
get_process_variables = request.data
print(get_task_id)
print(get_process_variables)
complete_task = CamundaWriteMixins.complete_task(url=CAMUNDA_URL, task_id=get_task_id,
process_variable_data=get_process_variables)
print("compl", complete_task)
return Response({"task_status": str(complete_task)})
except Exception as error:
return Response(error)
test.py
def test_completed_task(self):
self.client = Client()
url = reverse('complete-task')
data = {"variables": {
"dept_status": {"value": "approved", "type": "String"}}
}
response = self.client.post(url, data=data, params={"task_id": "000c29840512"},
headers={'Content-Type': 'application/json'})
print(response.data)
self.assertTrue(response.data)

我已经尝试了上面的测试用例方法,它正在获取请求数据,但我得到了参数None。提前感谢。

如果你只是修改你的请求一点,并添加查询参数作为你的url的一部分,那么我想你是很好的去。

例子:

response = self.client.post(f'{url}?task_id=000c29840512', data=data,
headers={'Content-Type': 'application/json'})

您可以参考官方文档中的示例:https://docs.djangoproject.com/en/4.0/topics/testing/tools/

最新更新