Package: incuna-test-utils
incuna-test-utilsIncuna Test Utils is a collection of TestCases and other helpers for testing
Django apps.TestCasesThese are found inincuna_test_utils.testcases.urls.URLTestCaseURLTestCaseaddsassert_url_matches_viewto check a url has been configured
to use the correct view.request.BaseRequestTestCaseBaseRequestTestCaseprovides 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.integration.BaseAdminIntegrationTestCaseBaseAdminIntegrationTestCaseprovides aTestCaseto test the django admin actions
such asadd,change,changelistanddelete.BaseAdminIntegrationTestCaseshould be subclassed and should define two attributes:auser_factoryto create an authenticated client;amodelto test.Example:fromincuna_test_utils.testcases.integrationimportBaseAdminIntegrationTestCaseclassTestUserAdmin(BaseAdminIntegrationTestCase):user_factory=factories.UserFactorymodel=ModelToTestdeftest_admin_add_page(self):response=self.get_admin_add_page()self.assertEqual(response.status_code,200)...integration.BaseIntegrationTestCaseBaseIntegrationTestCaseextendsBaseRequestTestCaseand 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.api_request.BaseAPIRequestTestCaseBaseAPIRequestTestCaseextendsBaseRequestTestCasefor use withdjango-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.FactoriesThese are found inincuna_test_utils.factories. They requirefactory_boy.user.BaseUserFactoryThis defines a simple factory with anemailattribute. This can be used with
a custom User model that has these fields:classUserFactory(BaseUserFactory):classMeta:model=Useruser.BaseAdminUserFactoryBaseAdminUserFactorydefines a user withis_active,is_staffandis_superusertoTrue. This factory also defines a post hook which
sets a default password accessible withraw_password.feincms_page.PageFactoryThis factory can be used to create instances ofFeincms'sPagemodel.compatcompatprovides 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