django-testcase 0.1.1
A TestCaseMixin utility.
License: MIT
Requires Python: >=3.4, <3.7
Keywords: tests,django

===============
django-testcase
===============

.. image:: https://travis-ci.org/jpmelos/django-testcase.svg?branch=master
    :target: https://travis-ci.org/jpmelos/django-testcase

 django-testcase provides a TestCase mixin to provide new useful functions for Django tests.

Supports Django 1.10 and later, only with Python 3.4 and later.

------------
Installation
------------

::

    $ pip install django-testcase

-----------
Basic usage
-----------

Simply subclass it in a ``TestCase``::

    from django.test import TestCase
    from django_testcase import TestCaseMixin

    class LoginViewTestCase(TestCaseMixin, TestCase):
        def test_login(self):
            pass

-----------------
Available methods
-----------------

``.create_user(username='test', email='test@test.com', password='test', is_superuser=False, *args, **kwargs)``
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Create a user with the given attributes. Any extra arguments passed will be passed to the creation call as is.

``.capture_output()``
^^^^^^^^^^^^^^^^^^^^

Allows testing Django's TestCase output system calls. Usage::

    with capture_output() as sys:
        self.statement()

In this situation, ``sys`` is a mocked object and can be tested with ``unittest.mock`` methods.

``.login(username='test', password='test', create_superuser=False)``
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Logs the user in. If the user does not exist, it will be created, taking ``is_superuser`` into consideration.

``.logout()``
^^^^^^^^^^^^

Logs the user out.

``.assertIsAuthenticated(user)``
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Asserts the given user is authenticated.

``.assertIsAnonymous()``
^^^^^^^^^^^^^^^^^^^^^^^^^^^

Assets there is no user authenticated.

``.assertRedirectsToLogin(response, next_url=None)``
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Asserts a given response redirects to the login page specified by ``settings.LOGIN_URL``, with a ``?next=`` URL if the parameter is given.

``.assertIssuedMessage(response, level, message)``
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Checks if the message ``message`` with level ``level`` has been issued. Only an exact match is accepted.

``.jpost(url, data)``
^^^^^^^^^^^^^^^^^^^^^

Sends ``data`` as a POST payload to ``url``.

``.create(klass, *args, **kwargs)``
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Creates an object of class ``klass`` with the passed arguments. Calls ``.full_clean()`` before saving.

-----------------
Development tasks
-----------------

Install for development and with dependencies::

    $ python install -e .[dev]

Asserting code quality
^^^^^^^^^^^^^^^^^^^^^^

::

    $ python dev.py quality

Packaging for PyPI distribution
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

::

    $ python dev.py package

The build will happen if the repository passes the quality tests and is clean: no untracked files are found. If not, you can see what's wrong with ``git clean -dfx --dry-run`` and remove or track and commit those files.

Builds will be available at ``dist``, in wheel and source.

----------
Contribute
----------

Pull requests are welcome.