Best approach to write unit tests involving rest apis.

96 views
Skip to first unread message

learn django

unread,
Dec 2, 2015, 6:12:18 PM12/2/15
to Django users
Hi,

My app uses rest framework.
I am writing test cases which involve standard requests like GET/POST/DELETE/PUT.
What is the best approach to write the test cases ?

Should I run the web server from unit test in setUp() (by running 'python manage.py runserver') so that http
request response can work. That way I can make sure my urls.py is correctly setup.

Or is there a better way to achieve the same ?

For now am calling my rest handlers directly by passing request (of type HttpRequest()) to it.
The request object is filled with required info which the backend code expects.

Since the handlers are called directly am unable to automate tests to make sure
urls are working fine.

Please give pointers as to how this can be done.
Thanks in advance.
  

Xavier Ordoquy

unread,
Dec 3, 2015, 1:48:52 AM12/3/15
to django...@googlegroups.com
Hi,

> Le 3 déc. 2015 à 00:12, learn django <siddhesh...@gmail.com> a écrit :
>
> My app uses rest framework.
> I am writing test cases which involve standard requests like GET/POST/DELETE/PUT.
> What is the best approach to write the test cases ?

Django REST framework comes with a test client named APIClient.
It will go through the middleware and urls before calling your API entry points which seems to be what you’re looking for.
The documentation about it is available at http://www.django-rest-framework.org/api-guide/testing/#apiclient

Regards,
Xavier,
Linovia.

signature.asc

learn django

unread,
Dec 3, 2015, 2:08:35 AM12/3/15
to Django users
Hi Linovia,

Yes, I started with that.
Initially I was getting 403 (auth failure) since my backend handlers have @login_required decorator.

To overcome that i created a super user (python manage.py superuser) &
used those credentials as below.

        # url = '/orca/api/v2/customer/'
        # data = {'c_name':'Pnc', 'c_role':'API-Admin'}
        # client = APIClient()
        # client.login(username='admin', password='admin')
        # response = client.post(url, data, format='json') 

After this I started was getting 302 (redirects).
Have you encountered similar issue before ?

Xavier Ordoquy

unread,
Dec 3, 2015, 5:06:26 AM12/3/15
to django...@googlegroups.com
Le 3 déc. 2015 à 08:08, learn django <siddhesh...@gmail.com> a écrit :

 On Wednesday, December 2, 2015 at 10:48:52 PM UTC-8, Xavier Ordoquy wrote:
Hi,

> Le 3 déc. 2015 à 00:12, learn django <siddhesh...@gmail.com> a écrit :
>
> My app uses rest framework.
> I am writing test cases which involve standard requests like GET/POST/DELETE/PUT.
> What is the best approach to write the test cases ?

Django REST framework comes with a test client named APIClient.
It will go through the middleware and urls before calling your API entry points which seems to be what you’re looking for.
The documentation about it is available at http://www.django-rest-framework.org/api-guide/testing/#apiclient


Hi Linovia,

Yes, I started with that.
Initially I was getting 403 (auth failure) since my backend handlers have @login_required decorator.

This may be an issue since a Django REST framework view shouldn’t use this decorator.
If it is a regular Django view that returns a JSON response, then you should consider using the Django client directly.

To overcome that i created a super user (python manage.py superuser) &
used those credentials as below.

        # url = '/orca/api/v2/customer/'
        # data = {'c_name':'Pnc', 'c_role':'API-Admin'}
        # client = APIClient()
        # client.login(username='admin', password='admin')
        # response = client.post(url, data, format='json') 

After this I started was getting 302 (redirects).
Have you encountered similar issue before ?

Where does it redirects you to ? If it’s the login page, it means that somehow the login call didn’t work.

Regards,
Xavier,
Linovia.
Reply all
Reply to author
Forward
0 new messages