incuna-test-utils


Nameincuna-test-utils JSON
Version 6.6.0 PyPI version JSON
home_pagehttps://github.com/incuna/incuna-test-utils/
SummaryCustom TestCases and other test helpers for Django apps
upload_time2016-10-07 14:50:21
maintainer
docs_urlNone
authorIncuna
requires_python
licenseBSD
keywords
VCS
bugtrack_url
requirements None django djangorestframework dj-database-url factory-boy feincms flake8 flake8-import-order mock psycopg2 py pytest pytest-cov pytest-django pytest-pythonpath
Travis-CI
Coveralis test coverage No Coveralis.
            # incuna-test-utils
[![Build Status](https://travis-ci.org/incuna/incuna-test-utils.svg?branch=master)](https://travis-ci.org/incuna/incuna-test-utils?branch=master)


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.
  * `view_instance` returns an instance of a class-based `view` attribute set
on the `TestCase` class. `view_instance` accepts a `request` and `*args` and
`**kwargs`. These are set on the `view` instance.
  * `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.BaseAdminIntegrationTestCase`


`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:
```python
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)

    ...
```


### `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_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` and `assert_count_multiple` run one or more assertions in
  a single method call.
  * `render_view_and_assert_content` and `render_view_and_assert_content_counts` combine
  a call to `access_view_and_render_response` with a multiple-assert call on the result.


### `api_request.BaseAPIRequestTestCase`


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

* `create_request` is overriden to use rest framework's
[`APIRequestFactory`](http://www.django-rest-framework.org/api-guide/testing#apirequestfactory).
It also sets `request.format` to `'json'`. If called with `auth=True` (the default),
`create_request` also calls
[`force_authenticate`](http://www.django-rest-framework.org/api-guide/testing#forcing-authentication).


## 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 an `email` attribute. This can be used with
a custom User model that has these fields:

```python
    class UserFactory(BaseUserFactory):
        class Meta:
            model = User
```


### `user.BaseAdminUserFactory`


`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`.


### `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.
  * `Python2AssertMixin` aliases python 2.7 assert methods to match the python 3 api.
    * `TestCase.assertItemsEqual` is aliased as `assertCountEqual`
    * `TestCase.assertRegexpMatches` is aliased as `assertRegex`

            

Raw data

            {
    "maintainer": "", 
    "docs_url": null, 
    "requires_python": "", 
    "maintainer_email": "", 
    "cheesecake_code_kwalitee_id": null, 
    "coveralis": false, 
    "keywords": "", 
    "upload_time": "2016-10-07 14:50:21", 
    "requirements": [
        {
            "name": null, 
            "specs": []
        }, 
        {
            "name": "django", 
            "specs": [
                [
                    ">=", 
                    "1.8"
                ], 
                [
                    "<", 
                    "1.9"
                ]
            ]
        }, 
        {
            "name": "djangorestframework", 
            "specs": [
                [
                    ">=", 
                    "3.0.5"
                ], 
                [
                    "<", 
                    "4"
                ]
            ]
        }, 
        {
            "name": "dj-database-url", 
            "specs": [
                [
                    "==", 
                    "0.4.1"
                ]
            ]
        }, 
        {
            "name": "factory-boy", 
            "specs": [
                [
                    "==", 
                    "2.7.0"
                ]
            ]
        }, 
        {
            "name": "feincms", 
            "specs": [
                [
                    "==", 
                    "1.12.1"
                ]
            ]
        }, 
        {
            "name": "flake8", 
            "specs": [
                [
                    "==", 
                    "2.5.4"
                ]
            ]
        }, 
        {
            "name": "flake8-import-order", 
            "specs": [
                [
                    "==", 
                    "0.7"
                ]
            ]
        }, 
        {
            "name": "mock", 
            "specs": [
                [
                    "==", 
                    "2.0.0"
                ]
            ]
        }, 
        {
            "name": "psycopg2", 
            "specs": [
                [
                    "==", 
                    "2.6.1"
                ]
            ]
        }, 
        {
            "name": "py", 
            "specs": [
                [
                    "==", 
                    "1.4.31"
                ]
            ]
        }, 
        {
            "name": "pytest", 
            "specs": [
                [
                    "==", 
                    "2.9.1"
                ]
            ]
        }, 
        {
            "name": "pytest-cov", 
            "specs": [
                [
                    "==", 
                    "2.2.1"
                ]
            ]
        }, 
        {
            "name": "pytest-django", 
            "specs": [
                [
                    "==", 
                    "2.9.1"
                ]
            ]
        }, 
        {
            "name": "pytest-pythonpath", 
            "specs": [
                [
                    "==", 
                    "0.7"
                ]
            ]
        }
    ], 
    "author": "Incuna", 
    "home_page": "https://github.com/incuna/incuna-test-utils/", 
    "github_user": "incuna", 
    "download_url": "https://pypi.python.org/packages/df/85/e98921b02c988e75a9ea97d39709b3c5e737c4490165f76caf161fca7f35/incuna-test-utils-6.6.0.tar.gz", 
    "platform": "", 
    "version": "6.6.0", 
    "cheesecake_documentation_id": null, 
    "description": "# incuna-test-utils\n[![Build Status](https://travis-ci.org/incuna/incuna-test-utils.svg?branch=master)](https://travis-ci.org/incuna/incuna-test-utils?branch=master)\n\n\nIncuna Test Utils is a collection of TestCases and other helpers for testing\nDjango apps.\n\n\n## TestCases\n\n\nThese are found in `incuna_test_utils.testcases`.\n\n\n### `urls.URLTestCase`\n\n\n`URLTestCase` adds `assert_url_matches_view` to check a url has been configured\nto use the correct view.\n\n\n### `request.BaseRequestTestCase`\n\n\n`BaseRequestTestCase` provides various helper methods for working with django\nviews:\n\n  * `get_view` returns a view callable based on a `view` attribute set on the\n`TestCase` class. `view` can be either a function-based or a class-based view.\n  * `view_instance` returns an instance of a class-based `view` attribute set\non the `TestCase` class. `view_instance` accepts a `request` and `*args` and\n`**kwargs`. These are set on the `view` instance.\n  * `add_session_to_request` gives a `request` a `session`.\n  * `create_user` returns a `user` using either `AnonymousUser` or a\n`user_factory` attribute set on the `TestCase`. The `user_factory` should have\na `create` method that returns a `user`.\n[`factory_boy`](http://factoryboy.readthedocs.org/en/latest/index.html) is recommended.\n  * `create_request` wraps Django's `RequestFactory` to provide useful defaults.\nIt returns a `request` with `user` and `_messages` attributes. It can also set\n`DATA` and `session` on the `request`.\n\n\n### `integration.BaseAdminIntegrationTestCase`\n\n\n`BaseAdminIntegrationTestCase` provides a `TestCase` to test the django admin actions\nsuch as `add`, `change`, `changelist` and `delete`.\n`BaseAdminIntegrationTestCase` should be subclassed and should define two attributes:\n - a `user_factory` to create an authenticated client;\n - a `model` to test.\n\nExample:\n```python\nfrom incuna_test_utils.testcases.integration import BaseAdminIntegrationTestCase\n\n\nclass TestUserAdmin(BaseAdminIntegrationTestCase):\n    user_factory = factories.UserFactory\n    model = ModelToTest\n\n    def test_admin_add_page(self):\n        response = self.get_admin_add_page()\n        self.assertEqual(response.status_code, 200)\n\n    ...\n```\n\n\n### `integration.BaseIntegrationTestCase`\n\n\n`BaseIntegrationTestCase` extends `BaseRequestTestCase` and adds more helper\nmethods useful for integration tests:\n\n  * `access_view` creates a `request`, calls the `TestCase`'s `view` and returns\na `response`.\n  * `render_to_str` renders a `response` using a `request`, `response.template_name`\nand `response.context_data`. If a `request` is not provided, `render_to_str` uses\n`response.request`.\n  * `access_view_and_render_response` wraps `access_view` and `render_to_str`.\nIt also checks the `response.status_code` is as expected. The default\n`expected_status` is `200` (`HTTP_OK`).\n  * `assert_presence` checks that an item does or doesn't appear in a container.\n  * `assert_count` checks that an item appears in a container an expected number\nof times.\n  * `assert_presence_multiple` and `assert_count_multiple` run one or more assertions in\n  a single method call.\n  * `render_view_and_assert_content` and `render_view_and_assert_content_counts` combine\n  a call to `access_view_and_render_response` with a multiple-assert call on the result.\n\n\n### `api_request.BaseAPIRequestTestCase`\n\n\n`BaseAPIRequestTestCase` extends `BaseRequestTestCase` for use with\n[`django-rest-framework`](http://www.django-rest-framework.org/).\n\n* `create_request` is overriden to use rest framework's\n[`APIRequestFactory`](http://www.django-rest-framework.org/api-guide/testing#apirequestfactory).\nIt also sets `request.format` to `'json'`. If called with `auth=True` (the default),\n`create_request` also calls\n[`force_authenticate`](http://www.django-rest-framework.org/api-guide/testing#forcing-authentication).\n\n\n## Factories\n\n\nThese are found in `incuna_test_utils.factories`. They require\n[`factory_boy`](http://factoryboy.readthedocs.org/en/latest/index.html).\n\n\n### `user.BaseUserFactory`\n\n\nThis defines a simple factory with an `email` attribute. This can be used with\na custom User model that has these fields:\n\n```python\n    class UserFactory(BaseUserFactory):\n        class Meta:\n            model = User\n```\n\n\n### `user.BaseAdminUserFactory`\n\n\n`BaseAdminUserFactory` defines a user with `is_active`, `is_staff` and\n`is_superuser` to `True`. This factory also defines a post hook which\nsets a default password accessible with `raw_password`.\n\n\n### `feincms_page.PageFactory`\n\n\nThis factory can be used to create instances of\n[`Feincms`](http://feincms-django-cms.readthedocs.org/en/latest/index.html)'s\n[`Page`](http://feincms-django-cms.readthedocs.org/en/latest/page.html) model.\n\n\n## `compat`\n\n\n`compat` provides a few miscelleaneous helpers useful for testing cross-version\ncode:\n\n  * `DJANGO_LT_15`, `DJANGO_LT_16`, `DJANGO_LT_17` each return `True` if\n`django.VERSION` is less than `1.5`, `1.6` or `1.7` respectively.\n  * `wipe_id_fieldson_django_lt_17` removes any field name ending in `_id` from\na collection if the django version is less than `1.7`. This is useful for testing\na model has the fields expected.\n  * `Python2AssertMixin` aliases python 2.7 assert methods to match the python 3 api.\n    * `TestCase.assertItemsEqual` is aliased as `assertCountEqual`\n    * `TestCase.assertRegexpMatches` is aliased as `assertRegex`\n", 
    "lcname": "incuna-test-utils", 
    "bugtrack_url": null, 
    "github": true, 
    "name": "incuna-test-utils", 
    "license": "BSD", 
    "travis_ci": true, 
    "github_project": "incuna-test-utils", 
    "summary": "Custom TestCases and other test helpers for Django apps", 
    "split_keywords": [], 
    "author_email": "admin@incuna.com", 
    "urls": [
        {
            "has_sig": false, 
            "upload_time": "2016-10-07T14:50:21", 
            "comment_text": "", 
            "python_version": "source", 
            "url": "https://pypi.python.org/packages/df/85/e98921b02c988e75a9ea97d39709b3c5e737c4490165f76caf161fca7f35/incuna-test-utils-6.6.0.tar.gz", 
            "md5_digest": "a150881d0c280436389dd029886d95a0", 
            "downloads": 0, 
            "filename": "incuna-test-utils-6.6.0.tar.gz", 
            "packagetype": "sdist", 
            "path": "df/85/e98921b02c988e75a9ea97d39709b3c5e737c4490165f76caf161fca7f35/incuna-test-utils-6.6.0.tar.gz", 
            "size": 12430
        }, 
        {
            "has_sig": false, 
            "upload_time": "2016-10-07T14:50:24", 
            "comment_text": "", 
            "python_version": "3.5", 
            "url": "https://pypi.python.org/packages/7a/d7/6dd14f924aa8571e73b99f2e84236df7dcb050474a7c91874c7d054db144/incuna_test_utils-6.6.0-py2.py3-none-any.whl", 
            "md5_digest": "f3bd82f647bd04a59a0bd7a47949361e", 
            "downloads": 0, 
            "filename": "incuna_test_utils-6.6.0-py2.py3-none-any.whl", 
            "packagetype": "bdist_wheel", 
            "path": "7a/d7/6dd14f924aa8571e73b99f2e84236df7dcb050474a7c91874c7d054db144/incuna_test_utils-6.6.0-py2.py3-none-any.whl", 
            "size": 19124
        }
    ], 
    "_id": null, 
    "cheesecake_installability_id": null
}