用于GET和DELETE的Django Rest Framework中API的单元测试示例

时间:2015-12-04 11:11:09

标签: django unit-testing django-rest-framework

我正在使用Django Rest Framework,我正在尝试在views.py文件中编写api方法的单元测试。这是http://www.django-rest-framework.org/api-guide/testing/#example给出一个例子但我不知道为api视图编写单元测试获取和删除 以下是我的api观点:

views.py

@api_view(['GET'])
def getAllCustomers(request):
    if request.method == 'GET':
        k = Customer.objects.all()
        serializer = CustomerSerializer(k, many=True)
        return Response(serializer.data)


@api_view(['DELETE'])
def deleteCustomer(request, pk):
    try:
        k = Customer.objects.get(pk=pk)
    except Customer.DoesNotExist:
        return HttpResponse(status=404)

    if request.method == 'DELETE':
        k.delete()
        return Response(status=status.HTTP_204_NO_CONTENT)

URLS.PY

url(r'^Customer/$', getAllCustomers, name='getAllCustomers'),
url(r'^Customer/del/(?P<pk>[0-9]+)$', deleteCustomer, name='deleteCustomer'),

感谢所有人!

0 个答案:

没有答案