0

Hi i am new in DJANGO and I have problems understanding how the unit tests should be designed for django. Please help me design a unit test case for following view.

def camera_gallery(request,gallery_id=None):
        #endpoint is url from where i get JSON data
    endpoint = settings.GALLERYAPI+'?galleryId='+ gallery_id+'&api_key=' + settings.GALLERY_API_KEY

    response = requests.get(endpoint)
    if response.status_code == 200:
        json_response = json.loads(response.content)
        context = json_response
    else:
        raise Http404
    return render(request, 'app/gallery_secondary.html',context)
Varun Chadha
  • 376
  • 2
  • 17
  • It is always up to discussion where to draw the limits when doing unit tests. In your case, the endpoint you are acessing (the GalleryAPI) should be tested by its own unit test. Testing a view that accesses another endpoint goes into the field of integration testing. – Michael May 25 '14 at 11:47
  • before creating your unit tests, make sure you correct one error: it's supposed to be `raise Http404()` and not `raise Http404` (notice the parantheses). – vlad-ardelean May 25 '14 at 11:47
  • Can you please suggest a test code for testing this view – Varun Chadha May 25 '14 at 12:27

1 Answers1

1

The Django testing tutorial has sample code for verifying the 200 vs. 404 return codes as well as checking that the response contains a particular string, so you should start with that.

Also, you might want to mock out the requests library with Mox so that you're not making actual HTTP requests to those services. This technique is known as dependency injection (see Wikipedia, SO, and Martin Fowler for more info).

Community
  • 1
  • 1
Misha Brukman
  • 12,938
  • 4
  • 61
  • 78