Skip to main content

A TestCaseMixin utility.

Project description

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


Project details


Download files

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

Filename, size & hash SHA256 hash help File type Python version Upload date
django_testcase-0.1.1-py3-none-any.whl (4.1 kB) Copy SHA256 hash SHA256 Wheel py3 May 23, 2018
django-testcase-0.1.1.tar.gz (4.9 kB) Copy SHA256 hash SHA256 Source None May 23, 2018

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page