2

As my first attempt in python to create a API test, my test file currently looks like this:

from django.test import TestCase, RequestFactory
from customer.models import Customer, CustomerStatus
from customer.views import CustomerPartialView
from rest_framework.test import APIRequestFactory


import pprint
from django.utils import timezone

class CustomerTest(TestCase) :

    def setUp(self) :
        self.factory = RequestFactory()

        self.cust_status = CustomerStatus.objects.create(
            status_id=1,name="banned",description="test desc" )

        self.customer = Customer.objects.create(
            guid='b27a3251-56e0-4870-8a03-27b0e92af9e5',
            created_date=timezone.now(),
            status_id=1,first_name='Hamster')



    def test_get_customer(self) :
        """                                                                                                         
        Ensure we can get a customer from db through the API                                                        
        """
        factory = APIRequestFactory()
        request = factory.get('/customer/b27a3251-56e0-4870-8a03-27b0e92af9e5')
        reponse = CustomerPartialView(request)

#in here I need something to check:
#response.data.first_name = "Hamster"

What I like to achieve is to insert dummy data into the database and then using APIRequestFactory to retrieve an individual customer record and make sure their first name matches what I expect.

I have managed to get this far but not sure what to do next.

My questions are:

  1. Am I on the right track?
  2. How do I test my result i.e. response.data.first_name = hamster ?
  3. Is there a better way of doing what I am trying to achieve

I am newbie to python so I apologise in advance if there are any major fails in my code.

Thanks

1
  • is there a concrete error or exception you are encountering? Commented Jun 5, 2015 at 6:56

3 Answers 3

1

If I understand right I think your problem could be that you're using response.data.first_name instead of response.data['first_name'], as response.data is a dict.

So should look like:

self.assertEqual(response.data['first_name'], 'Hamster')

Or, what I'd recommened:

self.assertEqual(response.data['first_name'], self.customer.first_name)

I'd also think it's a bit neater to change request url to '/customer/%s' % self.customer.guid Saves you re-writing the string.

Sign up to request clarification or add additional context in comments.

Comments

1

1) basic testing, looks good as a start.

2) check https://docs.djangoproject.com/en/1.8/topics/testing/overview/ for basic django testing. here, you would go on with the normal self.assertWhatever(..) procedure.

3) there are always other ways, wether they are better is the question...there are some tools that could make your testing easier, like mock or mixer (and many more). so, there might be a better way...

Comments

1

Thanks guys for your input.

I found out that I should be using APITestCase instead of TestCase for DRF.

I have Implemented both of your answers into my test script and I have got a working test script that looks like this:

from customer.models import Customer, CustomerStatus
from customer.views import CustomerPartialView

from django.core.urlresolvers import reverse
from rest_framework import status
from rest_framework.test import APITestCase

import pprint
from django.utils import timezone

class CustomerTest(APITestCase) :

    def setUp(self) :

    self.cust_status = CustomerStatus.objects.create(
            status_id=1,name="banned",description="test desc" )

    self.customer = Customer.objects.create(
            guid='b27a3251-56e0-4870-8a03-27b0e92af9e5',
            created_date=timezone.now(),
            status_id=1,first_name='Hamster')



    def test_get_customer(self) :
    """                                                                                                                                                                                                                              
        Ensure we can get a customer from db through the API                                                                                                                                                                             
        """
    response = self.client.get('/customer/%s/' % self.customer.guid)

    self.assertEqual(response.status_code, status.HTTP_200_OK)
    self.assertEqual(
            (response.data['first_name'],response.data['guid']),
            ('Hamster','b27a3251-56e0-4870-8a03-27b0e92af9e5'))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.