Incuna Test Utils is a collection of TestCases and other helpers for testing Django apps.
These are found in incuna_test_utils.testcases
.
URLTestCase
adds assert_url_matches_view
to check a url has been configured
to use the correct view.
BaseRequestTestCase
provides various helper methods for working with django
views:
get_view
returns a view callable based on aview
attribute set on theTestCase
class.view
can be either a function-based or a class-based view.view_instance
returns an instance of a class-basedview
attribute set on theTestCase
class.view_instance
accepts arequest
and*args
and**kwargs
. These are set on theview
instance.add_session_to_request
gives arequest
asession
.create_user
returns auser
using eitherAnonymousUser
or auser_factory
attribute set on theTestCase
. Theuser_factory
should have acreate
method that returns auser
.factory_boy
is recommended.create_request
wraps Django'sRequestFactory
to provide useful defaults. It returns arequest
withuser
and_messages
attributes. It can also setDATA
andsession
on therequest
.
BaseAdminIntegrationTestCase
provides a TestCase
to test the django admin actions
such as add
, change
, changelist
and delete
.
BaseAdminIntegrationTestCase
should be subclassed and should define two attributes:
- a
user_factory
to create an authenticated client; - a
model
to test.
Example:
from incuna_test_utils.testcases.integration import BaseAdminIntegrationTestCase
class TestUserAdmin(BaseAdminIntegrationTestCase):
user_factory = factories.UserFactory
model = ModelToTest
def test_admin_add_page(self):
response = self.get_admin_add_page()
self.assertEqual(response.status_code, 200)
...
BaseIntegrationTestCase
extends BaseRequestTestCase
and adds more helper
methods useful for integration tests:
access_view
creates arequest
, calls theTestCase
'sview
and returns aresponse
.render_to_str
renders aresponse
using arequest
,response.template_name
andresponse.context_data
. If arequest
is not provided,render_to_str
usesresponse.request
.access_view_and_render_response
wrapsaccess_view
andrender_to_str
. It also checks theresponse.status_code
is as expected. The defaultexpected_status
is200
(HTTP_OK
).assert_presence
checks that an item does or doesn't appear in a container.assert_count
checks that an item appears in a container an expected number of times.assert_presence_multiple
andassert_count_multiple
run one or more assertions in a single method call.render_view_and_assert_content
andrender_view_and_assert_content_counts
combine a call toaccess_view_and_render_response
with a multiple-assert call on the result.
BaseAPIRequestTestCase
extends BaseRequestTestCase
for use with
django-rest-framework
.
create_request
is overriden to use rest framework'sAPIRequestFactory
. It also setsrequest.format
to'json'
. If called withauth=True
(the default),create_request
also callsforce_authenticate
.
These are found in incuna_test_utils.factories
. They require
factory_boy
.
This defines a simple factory with an email
attribute. This can be used with
a custom User model that has these fields:
class UserFactory(BaseUserFactory):
class Meta:
model = User
BaseAdminUserFactory
defines a user with is_active
, is_staff
and
is_superuser
to True
. This factory also defines a post hook which
sets a default password accessible with raw_password
.
This factory can be used to create instances of
Feincms
's
Page
model.
compat
provides a few miscelleaneous helpers useful for testing cross-version
code:
DJANGO_LT_19
isTrue
ifdjango.VERSION
is less than1.9
.Python2AssertMixin
aliases python 2.7 assert methods to match the python 3 api.TestCase.assertItemsEqual
is aliased asassertCountEqual
TestCase.assertRegexpMatches
is aliased asassertRegex