Django REST Assured

Instantly test-cover your Django REST Framework based API


Testing sucks

Can't write them, can't live without them.

So much boilerplate

Our tests still contain tons of non-DRY code.

REST API solved

We can define an entire CRUD endpoint with just 3 trivial class declarations.

I can train a chicken

To test my trivial code.


Testing sucks

So much.

Django REST Assured



Sanity coverage

It's a "Django system check framework" for your API.

Extensible boilerplate

So you can write less of it.

Built for REST

Choose the mixins you need:



from django.db import models

class Blog(models.Model):

    name = models.CharField(max_length=100)
    tagline = models.TextField()

    def __unicode__(self):

class Author(models.Model):

    name = models.CharField(max_length=50)
    email = models.EmailField()

    def __unicode__(self):
class Entry(models.Model):

    blog = models.ForeignKey(Blog)
    headline = models.CharField(max_length=255)
    body_text = models.TextField()
    pub_date = models.DateField()
    mod_date = models.DateField(auto_now=True)
    authors = models.ManyToManyField(Author)
    n_comments = models.IntegerField()
    n_pingbacks = models.IntegerField()
    rating = models.IntegerField()

    def __unicode__(self):
        return self.headline

from rest_framework import serializers
from . import models

class Entry(serializers.ModelSerializer):

    class Meta:
        model = models.Entry

from rest_framework import viewsets
from . import models, serializers

class Entries(viewsets.ModelViewSet):

    queryset = models.Entry.objects.all()
    serializer_class = serializers.Entry

from django.conf.urls import url, include
from rest_framework import routers
from . import views

router = routers.DefaultRouter()
router.register(r'entries', views.Entries)

urlpatterns = [
    url(r'^', include(router.urls)),

import datetime
import factory
from factory import fuzzy
from django.contrib import auth
from . import models

class Blog(factory.DjangoModelFactory):

    class Meta:
        model = models.Blog

    # fields...

class Author(factory.DjangoModelFactory):

    class Meta:
        model = models.Author

    # fields...
class Entry(factory.DjangoModelFactory):

    class Meta:
        model = models.Entry

    # fields...

class User(factory.DjangoModelFactory):

    class Meta:

        model = auth.get_user_model()

    # fields...

import datetime
from rest_assured.testcases import ReadWriteRESTAPITestCaseMixin, BaseRESTAPITestCase
from . import factories

class EntryAPITestCase(ReadWriteRESTAPITestCaseMixin, BaseRESTAPITestCase):

    base_name = 'entry' # this is the base_name generated by the DefaultRouter
    factory_class = factories.Entry
    user_factory = factories.User
    update_data = {'rating': 5}

    def setUp(self): = factories.Author.create()
       super(EntryAPITestCase, self).setUp()

    def get_object(self, factory):
        return factory.create(authors=[])

    def get_create_data(self):
       return {'headline': 'Lucifer Sam',
               'body_text': 'is a song by British psychedelic rock band Pink Floyd.',
               'authors': [],
               'rating': 4,
               'n_pingbacks': 0,
               'n_comments': 0,
               'pub_date':, 11, 12),

In this example we tested:

GET /api/entries/

GET /api/entries/:id/

POST /api/entries/

PATCH /api/entries/:id/

DELETE /api/entries/:id/



1 active contributor (@maryokhin)

PyPI: django-rest-assured

~2K/month  downloads


Python: 2.7 & 3.4

Django: 1.7 (probably 1.6 & 1.5)

DRF: 2.4.x

Go grab it!

Thank you.


Django REST Assured Intro

By ydaniv

Django REST Assured Intro

An introduction to Django REST Assured.

  • 1,414