Skip to main content

Custom TestCases and other test helpers for Django apps

Project description

# incuna-test-utils

Incuna Test Utils is a collection of TestCases and other helpers for testing Django apps.

## TestCases

These are found in incuna_test_utils.testcases.

### urls.URLTestCase

URLTestCase adds assert_url_matches_view to check a url has been configured to use the correct view.

### request.BaseRequestTestCase

BaseRequestTestCase provides various helper methods for working with django views:

  • get_view returns a view callable based on a view attribute set on the TestCase class. view can be either a function-based or a class-based view.

  • add_session_to_request gives a request a session.

  • create_user returns a user using either AnonymousUser or a user_factory attribute set on the TestCase. The user_factory should have a create method that returns a user. [factory_boy](http://factoryboy.readthedocs.org/en/latest/index.html) is recommended.

  • create_request wraps Django’s RequestFactory to provide useful defaults. It returns a request with user and _messages attributes. It can also set DATA and session on the request.

### integration.BaseIntegrationTestCase

BaseIntegrationTestCase extends BaseRequestTestCase and adds more helper methods useful for integration tests:

  • access_view creates a request, calls the TestCase’s view and returns a response.

  • render_to_str renders a response using a request, response.template_name and response.context_data. If a request is not provided, render_to_str uses response.request.

  • access_view_and_render_response wraps access_view and render_to_str. It also checks the response.status_code is as expected. The default expected_status is 200 (HTTP_OK).

  • assert_count checks that an item appears in a container an expected number of times.

### api_request.BaseAPIRequestTestCase

BaseAPIRequestTestCase extends BaseRequestTestCase for use with [django-rest-framework](http://www.django-rest-framework.org/).

## Factories

These are found in incuna_test_utils.factories. They require [factory_boy](http://factoryboy.readthedocs.org/en/latest/index.html).

### user.BaseUserFactory

This defines a simple factory with email and name attributes. This can be used with a custom User model that has these fields:

class UserFactory(BaseUserFactory):
class Meta:

model = User

### feincms_page.PageFactory

This factory can be used to create instances of [Feincms](http://feincms-django-cms.readthedocs.org/en/latest/index.html)’s [Page](http://feincms-django-cms.readthedocs.org/en/latest/page.html) model.

## compat

compat provides a few miscelleaneous helpers useful for testing cross-version code:

  • DJANGO_LT_15, DJANGO_LT_16, DJANGO_LT_17 each return True if django.VERSION is less than 1.5, 1.6 or 1.7 respectively.

  • wipe_id_fieldson_django_lt_17 removes any field name ending in _id from a collection if the django version is less than 1.7. This is useful for testing a model has the fields expected.

  • Python2CountEqualMixin aliases the python 2.7 TestCase.assertItemsEqual method as assertCountEqual to match the python 3 api.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

incuna-test-utils-5.0.0.tar.gz (8.6 kB view details)

Uploaded Source

Built Distribution

incuna_test_utils-5.0.0-py2.py3-none-any.whl (17.1 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file incuna-test-utils-5.0.0.tar.gz.

File metadata

File hashes

Hashes for incuna-test-utils-5.0.0.tar.gz
Algorithm Hash digest
SHA256 083817166a7491c4669102e5a3311f1cf8030bfdfb5ebbeff6858f22b4c792ac
MD5 44237c2ce0cdc58dcc8c9fcee77fc46d
BLAKE2b-256 b02088519b25f5c0ecabbfd10143adafed5a1c0c3a6d11139900ea66f7596b00

See more details on using hashes here.

File details

Details for the file incuna_test_utils-5.0.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for incuna_test_utils-5.0.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 c1001e0a515516780dc6f52367755090c0dca92641ca7c157048d3844665963e
MD5 9f6efd515fb7651af3ad2cb03f354d95
BLAKE2b-256 aa97d610a4e5da568e26210da9da56378d8d4cdf8f88696c80b76c4323919b93

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page