Code Monkey home page Code Monkey logo

django-test-mixins's Introduction

django-test-mixins

Additional assertions and test conveniences for testing django sites.

Build Status Coverage Status Latest Version License

Table of Contents

Usage

All classes inherit from django.test.TestCase so you can access the Django assertions and the HTTP client as usual.

django-test-mixins is available on PyPI. Installation is simply a matter of:

$ pip install django_test_mixins

HttpCodeTestCase

HttpCodeTestCase provides the following assertions for exact HTTP responses:

  • assertHttpOK(response) (200)
  • assertHttpCreated(response) (201)
  • assertHttpBadRequest(response) (400)
  • assertHttpUnauthorized(response) (401)
  • assertHttpForbidden(response) (403)
  • assertHttpNotFound(response) (404)
  • assertHttpMethodNotAllowed(response) (405)

It also provides the following assertions for groups of HTTP responses:

  • assertHttpRedirect(response, location=None) (3XX)

Example:

from django.core.urlresolvers import reverse

from django_test_mixins import HttpCodeTestCase


class TestIndex(HttpCodeTestCase):
    def test_home_page(self):
        response = self.client.get(reverse('index'))
        self.assertHttpOK(response)

EmptyCacheTestCase

EmptyCacheTestCase provides no extra assertions, but ensures that every test starts with a empty cache.

FormValidationTestCase

FormValidationTestCase provides the following assertion:

  • assertFormInvalid(response, form_name="form")

Example:

Assume we have a view that looks like this:

from django.shortcuts import render, redirect

from .forms import HouseForm


def create_house(request):
    if request.method == 'POST':
        form = HouseForm(request.POST)

        if form.is_valid():
            form.save()
            return redirect('index')
    else:
        form = HouseForm()

    return render(request, "create_house.html", {'house_form': form})

We can then write a test like this. Note that form_name needs to match the context we rendered the page with (defaults to "form").

from django.core.urlresolvers import reverse

from django_test_mixins import FormValidationTestCase


class TestCreateHouse(FormValidationTestCase):
    def test_create_requires_name(self):
        response = self.client.post(reverse('create_house'), {})
        self.assertFormInvalid(response, form_name="house_form")

RedirectTestCase

RedirectTestCase provides the following assertions:

  • assertRedirectsTo(response, expected_url)

Django has TestCase.assertRedirects (docs) but this does not support redirects to external URLs because of limitations in the test client.

assertRedirectsTo does not support chained redirects, so you can't do self.client.get(SOME_URL, follow=True).

Example:

from django.core.urlresolvers import reverse

from django_test_mixins import FormValidationTestCase


class TestIndex(RedirectTestCase):
    def test_home_redirects_to_login(self):
        """If the user isn't logged in, index should redirect to the login page.

        """
        response = self.client.get(reverse('index'))
        self.assertRedirectsTo(response, reverse('log_in'))

Combining test cases

You can freely combine these classes by simply inheriting from multiple classes.

from django.core.urlresolvers import reverse

from django_test_mixins import FreshCacheTestCase, HttpCodeTestCase


class TestIndex(FreshCacheTestCase, HttpCodeTestCase):
    def test_home_page(self):
        response = self.client.get(reverse('index'))
        self.assertHttpOK(response)

Uploading to PyPI

Releasing a new version is a matter of:

$ python setup.py sdist upload

django-test-mixins's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.