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_viewreturns a view callable based on aviewattribute set on theTestCaseclass.viewcan be either a function-based or a class-based view.view_instancereturns an instance of a class-basedviewattribute set on theTestCaseclass.view_instanceaccepts arequestand*argsand**kwargs. These are set on theviewinstance.add_session_to_requestgives arequestasession.create_userreturns auserusing eitherAnonymousUseror auser_factoryattribute set on theTestCase. Theuser_factoryshould have acreatemethod that returns auser.factory_boyis recommended.create_requestwraps Django'sRequestFactoryto provide useful defaults. It returns arequestwithuserand_messagesattributes. It can also setDATAandsessionon 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_factoryto create an authenticated client; - a
modelto 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_viewcreates arequest, calls theTestCase'sviewand returns aresponse.render_to_strrenders aresponseusing arequest,response.template_nameandresponse.context_data. If arequestis not provided,render_to_strusesresponse.request.access_view_and_render_responsewrapsaccess_viewandrender_to_str. It also checks theresponse.status_codeis as expected. The defaultexpected_statusis200(HTTP_OK).assert_presencechecks that an item does or doesn't appear in a container.assert_countchecks that an item appears in a container an expected number of times.assert_presence_multipleandassert_count_multiplerun one or more assertions in a single method call.render_view_and_assert_contentandrender_view_and_assert_content_countscombine a call toaccess_view_and_render_responsewith a multiple-assert call on the result.
BaseAPIRequestTestCase extends BaseRequestTestCase for use with
django-rest-framework.
create_requestis overriden to use rest framework'sAPIRequestFactory. It also setsrequest.formatto'json'. If called withauth=True(the default),create_requestalso 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 = UserBaseAdminUserFactory 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_19isTrueifdjango.VERSIONis less than1.9.Python2AssertMixinaliases python 2.7 assert methods to match the python 3 api.TestCase.assertItemsEqualis aliased asassertCountEqualTestCase.assertRegexpMatchesis aliased asassertRegex