test_plus package

Submodules

test_plus.runner module

class test_plus.runner.NoLoggingRunner(pattern=None, top_level=None, verbosity=1, interactive=True, failfast=False, keepdb=False, reverse=False, debug_mode=False, debug_sql=False, parallel=0, tags=None, exclude_tags=None, **kwargs)[source]

Bases: django.test.runner.DiscoverRunner

run_tests(test_labels, extra_tests=None, **kwargs)[source]

Run the unit tests for all the test labels in the provided list.

Test labels should be dotted Python paths to test modules, test classes, or test methods.

A list of ‘extra’ tests may also be provided; these tests will be added to the test suite.

Returns the number of tests that failed.

test_plus.test module

class test_plus.test.APITestCase(*args, **kwargs)[source]

Bases: test_plus.test.TestCase

class test_plus.test.BaseTestCase(*args, **kwargs)[source]

Bases: object

Django TestCase with helpful additional features

assertContext(key, value)[source]
assertGoodView(url_name, *args, **kwargs)[source]

Quick-n-dirty testing of a given url name. Ensures URL returns a 200 status and that generates less than 50 database queries.

assertInContext(key)[source]
assertLoginRequired(url, *args, **kwargs)[source]

Ensure login is required to GET this URL

assertNumQueriesLessThan(num, *args, **kwargs)[source]
assertResponseContains(text, response=None, html=True, **kwargs)[source]

Convenience wrapper for assertContains

assertResponseHeaders(headers, response=None)[source]

Check that the headers in the response are as expected.

Only headers defined in headers are compared, other keys present on the response will be ignored.

Parameters:
  • headers (collections.Mapping) – Mapping of header names to expected values
  • response (django.http.response.HttpResponse) – Response to check headers against
assertResponseNotContains(text, response=None, html=True, **kwargs)[source]

Convenience wrapper for assertNotContains

delete(url_name, *args, **kwargs)[source]
get(url_name, *args, **kwargs)[source]
get_check_200(url, *args, **kwargs)[source]

Test that we can GET a page and it returns a 200

get_context(key)[source]
head(url_name, *args, **kwargs)[source]
login(*args, **credentials)[source]

Login a user

make_user(username='testuser', password='password', perms=None)[source]

Build a user with <username> and password of ‘password’ for testing purposes.

options(url_name, *args, **kwargs)[source]
patch(url_name, *args, **kwargs)[source]
post(url_name, *args, **kwargs)[source]
print_form_errors(response_or_form=None)[source]

A utility method for quickly debugging responses with form errors.

put(url_name, *args, **kwargs)[source]
request(method_name, url_name, *args, **kwargs)[source]

Request url by name using reverse() through method

If reverse raises NoReverseMatch attempt to use it as a URL.

response_200(response=None, msg=None)[source]

Given response has status_code 200

response_201(response=None, msg=None)[source]

Given response has status_code 201

response_204(response=None, msg=None)[source]

Given response has status_code 204

response_301(response=None, msg=None)[source]

Given response has status_code 301

response_302(response=None, msg=None)[source]

Given response has status_code 302

response_400(response=None, msg=None)[source]

Given response has status_code 400

response_401(response=None, msg=None)[source]

Given response has status_code 401

response_403(response=None, msg=None)[source]

Given response has status_code 403

response_404(response=None, msg=None)[source]

Given response has status_code 404

response_405(response=None, msg=None)[source]

Given response has status_code 405

response_410(response=None, msg=None)[source]

Given response has status_code 410

reverse(name, *args, **kwargs)[source]

Reverse a url, convenience to avoid having to import reverse in tests

tearDown()[source]
trace(url_name, *args, **kwargs)[source]
user_factory = None
class test_plus.test.CBVTestCase(*args, **kwargs)[source]

Bases: test_plus.test.TestCase

Directly calls class-based generic view methods, bypassing the Django test Client.

This process bypasses middleware invocation and URL resolvers.

Example usage:

from myapp.views import MyClass

class MyClassTest(CBVTestCase):

def test_special_method(self):

request = RequestFactory().get(‘/’) instance = self.get_instance(MyClass, request=request)

# invoke a MyClass method result = instance.special_method()

# make assertions self.assertTrue(result)

assertGoodView(url_name, *args, **kwargs)[source]

Quick-n-dirty testing of a given view. Ensures view returns a 200 status and that generates less than 50 database queries.

assertLoginRequired(url, *args, **kwargs)[source]

Ensure login is required to GET this URL

get(cls, *args, **kwargs)[source]

Calls cls.get() method after instantiating view class. Renders view templates and sets context if appropriate.

get_check_200(url, *args, **kwargs)[source]

Test that we can GET a page and it returns a 200

get_instance(cls, *args, **kwargs)[source]

Returns a decorated instance of a class-based generic view class.

Use initkwargs to set expected class attributes. For example, set the object attribute on MyDetailView class:

instance = self.get_instance(MyDetailView, initkwargs={‘object’: obj}, request)

because SingleObjectMixin (part of generic.DetailView) expects self.object to be set before invoking get_context_data().

Pass a “request” kwarg in order for your tests to have particular request attributes.

get_response(request, view_func)[source]

Obtain response from view class method (typically get or post).

No middleware is invoked, but templates are rendered and context saved if appropriate.

post(cls, *args, **kwargs)[source]

Calls cls.post() method after instantiating view class. Renders view templates and sets context if appropriate.

exception test_plus.test.NoPreviousResponse[source]

Bases: exceptions.Exception

class test_plus.test.TestCase(*args, **kwargs)[source]

Bases: django.test.testcases.TestCase, test_plus.test.BaseTestCase

Django TestCase with helpful additional features

user_factory = None
class test_plus.test.login(testcase, *args, **credentials)[source]

Bases: object

A useful login context for Django tests. If the first argument is a User, we will login with that user’s username. If no password is given we will use ‘password’.

Module contents

class test_plus.APITestCase(*args, **kwargs)[source]

Bases: test_plus.test.TestCase

class test_plus.TestCase(*args, **kwargs)[source]

Bases: django.test.testcases.TestCase, test_plus.test.BaseTestCase

Django TestCase with helpful additional features

user_factory = None