2012-12-22 00:59:06 +00:00
|
|
|
=======================
|
|
|
|
Advanced testing topics
|
|
|
|
=======================
|
|
|
|
|
|
|
|
The request factory
|
|
|
|
===================
|
|
|
|
|
2013-09-09 08:59:47 +00:00
|
|
|
.. currentmodule:: django.test
|
2012-12-22 00:59:06 +00:00
|
|
|
|
|
|
|
.. class:: RequestFactory
|
|
|
|
|
2013-09-09 08:59:47 +00:00
|
|
|
The :class:`~django.test.RequestFactory` shares the same API as
|
2012-12-22 00:59:06 +00:00
|
|
|
the test client. However, instead of behaving like a browser, the
|
|
|
|
RequestFactory provides a way to generate a request instance that can
|
|
|
|
be used as the first argument to any view. This means you can test a
|
|
|
|
view function the same way as you would test any other function -- as
|
|
|
|
a black box, with exactly known inputs, testing for specific outputs.
|
|
|
|
|
2013-09-09 08:59:47 +00:00
|
|
|
The API for the :class:`~django.test.RequestFactory` is a slightly
|
2012-12-22 00:59:06 +00:00
|
|
|
restricted subset of the test client API:
|
|
|
|
|
|
|
|
* It only has access to the HTTP methods :meth:`~Client.get()`,
|
|
|
|
:meth:`~Client.post()`, :meth:`~Client.put()`,
|
2014-10-13 11:10:00 +00:00
|
|
|
:meth:`~Client.delete()`, :meth:`~Client.head()`,
|
|
|
|
:meth:`~Client.options()`, and :meth:`~Client.trace()`.
|
2012-12-22 00:59:06 +00:00
|
|
|
|
|
|
|
* These methods accept all the same arguments *except* for
|
2017-12-04 09:23:16 +00:00
|
|
|
``follow``. Since this is just a factory for producing
|
2012-12-22 00:59:06 +00:00
|
|
|
requests, it's up to you to handle the response.
|
|
|
|
|
|
|
|
* It does not support middleware. Session and authentication
|
|
|
|
attributes must be supplied by the test itself if required
|
|
|
|
for the view to function properly.
|
|
|
|
|
|
|
|
Example
|
|
|
|
-------
|
|
|
|
|
2019-06-17 14:54:55 +00:00
|
|
|
The following is a unit test using the request factory::
|
2012-12-22 00:59:06 +00:00
|
|
|
|
2014-10-20 14:51:25 +00:00
|
|
|
from django.contrib.auth.models import AnonymousUser, User
|
2018-05-12 17:37:42 +00:00
|
|
|
from django.test import RequestFactory, TestCase
|
2012-12-22 00:59:06 +00:00
|
|
|
|
2015-10-30 15:27:49 +00:00
|
|
|
from .views import MyView, my_view
|
2015-02-17 18:53:20 +00:00
|
|
|
|
2013-07-02 08:19:44 +00:00
|
|
|
class SimpleTest(TestCase):
|
2012-12-22 00:59:06 +00:00
|
|
|
def setUp(self):
|
|
|
|
# Every test needs access to the request factory.
|
|
|
|
self.factory = RequestFactory()
|
2013-07-02 08:19:44 +00:00
|
|
|
self.user = User.objects.create_user(
|
2013-10-22 16:05:26 +00:00
|
|
|
username='jacob', email='jacob@…', password='top_secret')
|
2012-12-22 00:59:06 +00:00
|
|
|
|
|
|
|
def test_details(self):
|
|
|
|
# Create an instance of a GET request.
|
|
|
|
request = self.factory.get('/customer/details')
|
|
|
|
|
2014-04-22 23:05:14 +00:00
|
|
|
# Recall that middleware are not supported. You can simulate a
|
2013-07-02 08:19:44 +00:00
|
|
|
# logged-in user by setting request.user manually.
|
|
|
|
request.user = self.user
|
|
|
|
|
2014-10-20 14:51:25 +00:00
|
|
|
# Or you can simulate an anonymous user by setting request.user to
|
|
|
|
# an AnonymousUser instance.
|
|
|
|
request.user = AnonymousUser()
|
|
|
|
|
2012-12-22 00:59:06 +00:00
|
|
|
# Test my_view() as if it were deployed at /customer/details
|
|
|
|
response = my_view(request)
|
2015-10-30 15:27:49 +00:00
|
|
|
# Use this syntax for class-based views.
|
|
|
|
response = MyView.as_view()(request)
|
2012-12-22 00:59:06 +00:00
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
2020-02-12 22:15:00 +00:00
|
|
|
AsyncRequestFactory
|
|
|
|
-------------------
|
|
|
|
|
|
|
|
``RequestFactory`` creates WSGI-like requests. If you want to create ASGI-like
|
|
|
|
requests, including having a correct ASGI ``scope``, you can instead use
|
|
|
|
``django.test.AsyncRequestFactory``.
|
|
|
|
|
|
|
|
This class is directly API-compatible with ``RequestFactory``, with the only
|
|
|
|
difference being that it returns ``ASGIRequest`` instances rather than
|
|
|
|
``WSGIRequest`` instances. All of its methods are still synchronous callables.
|
|
|
|
|
2019-12-04 15:36:42 +00:00
|
|
|
Testing class-based views
|
|
|
|
=========================
|
|
|
|
|
|
|
|
In order to test class-based views outside of the request/response cycle you
|
|
|
|
must ensure that they are configured correctly, by calling
|
|
|
|
:meth:`~django.views.generic.base.View.setup` after instantiation.
|
|
|
|
|
|
|
|
For example, assuming the following class-based view:
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
:caption: views.py
|
|
|
|
|
|
|
|
from django.views.generic import TemplateView
|
|
|
|
|
|
|
|
|
|
|
|
class HomeView(TemplateView):
|
|
|
|
template_name = 'myapp/home.html'
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
kwargs['environment'] = 'Production'
|
|
|
|
return super().get_context_data(**kwargs)
|
|
|
|
|
|
|
|
You may directly test the ``get_context_data()`` method by first instantiating
|
|
|
|
the view, then passing a ``request`` to ``setup()``, before proceeding with
|
|
|
|
your test's code:
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
:caption: tests.py
|
|
|
|
|
|
|
|
from django.test import RequestFactory, TestCase
|
|
|
|
from .views import HomeView
|
|
|
|
|
|
|
|
|
|
|
|
class HomePageTest(TestCase):
|
|
|
|
def test_environment_set_in_context(self):
|
|
|
|
request = RequestFactory().get('/')
|
|
|
|
view = HomeView()
|
|
|
|
view.setup(request)
|
|
|
|
|
|
|
|
context = view.get_context_data()
|
|
|
|
self.assertIn('environment', context)
|
|
|
|
|
2016-06-03 22:02:38 +00:00
|
|
|
.. _topics-testing-advanced-multiple-hosts:
|
|
|
|
|
|
|
|
Tests and multiple host names
|
|
|
|
=============================
|
|
|
|
|
|
|
|
The :setting:`ALLOWED_HOSTS` setting is validated when running tests. This
|
|
|
|
allows the test client to differentiate between internal and external URLs.
|
|
|
|
|
|
|
|
Projects that support multitenancy or otherwise alter business logic based on
|
|
|
|
the request's host and use custom host names in tests must include those hosts
|
|
|
|
in :setting:`ALLOWED_HOSTS`.
|
|
|
|
|
2019-06-17 14:54:55 +00:00
|
|
|
The first option to do so is to add the hosts to your settings file. For
|
|
|
|
example, the test suite for docs.djangoproject.com includes the following::
|
2016-06-03 22:02:38 +00:00
|
|
|
|
|
|
|
from django.test import TestCase
|
|
|
|
|
|
|
|
class SearchFormTestCase(TestCase):
|
|
|
|
def test_empty_get(self):
|
|
|
|
response = self.client.get('/en/dev/search/', HTTP_HOST='docs.djangoproject.dev:8000')
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
|
|
|
and the settings file includes a list of the domains supported by the project::
|
|
|
|
|
|
|
|
ALLOWED_HOSTS = [
|
|
|
|
'www.djangoproject.dev',
|
|
|
|
'docs.djangoproject.dev',
|
|
|
|
...
|
|
|
|
]
|
|
|
|
|
|
|
|
Another option is to add the required hosts to :setting:`ALLOWED_HOSTS` using
|
|
|
|
:meth:`~django.test.override_settings()` or
|
|
|
|
:attr:`~django.test.SimpleTestCase.modify_settings()`. This option may be
|
|
|
|
preferable in standalone apps that can't package their own settings file or
|
|
|
|
for projects where the list of domains is not static (e.g., subdomains for
|
|
|
|
multitenancy). For example, you could write a test for the domain
|
|
|
|
``http://otherserver/`` as follows::
|
|
|
|
|
|
|
|
from django.test import TestCase, override_settings
|
|
|
|
|
|
|
|
class MultiDomainTestCase(TestCase):
|
|
|
|
@override_settings(ALLOWED_HOSTS=['otherserver'])
|
|
|
|
def test_other_domain(self):
|
|
|
|
response = self.client.get('http://otherserver/foo/bar/')
|
|
|
|
|
|
|
|
Disabling :setting:`ALLOWED_HOSTS` checking (``ALLOWED_HOSTS = ['*']``) when
|
|
|
|
running tests prevents the test client from raising a helpful error message if
|
|
|
|
you follow a redirect to an external URL.
|
|
|
|
|
2012-12-22 00:59:06 +00:00
|
|
|
.. _topics-testing-advanced-multidb:
|
|
|
|
|
|
|
|
Tests and multiple databases
|
|
|
|
============================
|
|
|
|
|
2014-05-20 19:54:56 +00:00
|
|
|
.. _topics-testing-primaryreplica:
|
2012-12-22 00:59:06 +00:00
|
|
|
|
2014-05-20 19:54:56 +00:00
|
|
|
Testing primary/replica configurations
|
2014-05-25 23:25:43 +00:00
|
|
|
--------------------------------------
|
2012-12-22 00:59:06 +00:00
|
|
|
|
2014-05-20 19:54:56 +00:00
|
|
|
If you're testing a multiple database configuration with primary/replica
|
|
|
|
(referred to as master/slave by some databases) replication, this strategy of
|
|
|
|
creating test databases poses a problem.
|
2012-12-22 00:59:06 +00:00
|
|
|
When the test databases are created, there won't be any replication,
|
2014-05-20 19:54:56 +00:00
|
|
|
and as a result, data created on the primary won't be seen on the
|
|
|
|
replica.
|
2012-12-22 00:59:06 +00:00
|
|
|
|
|
|
|
To compensate for this, Django allows you to define that a database is
|
|
|
|
a *test mirror*. Consider the following (simplified) example database
|
|
|
|
configuration::
|
|
|
|
|
|
|
|
DATABASES = {
|
|
|
|
'default': {
|
|
|
|
'ENGINE': 'django.db.backends.mysql',
|
|
|
|
'NAME': 'myproject',
|
2014-05-20 19:54:56 +00:00
|
|
|
'HOST': 'dbprimary',
|
2012-12-22 00:59:06 +00:00
|
|
|
# ... plus some other settings
|
|
|
|
},
|
2014-05-20 19:54:56 +00:00
|
|
|
'replica': {
|
2012-12-22 00:59:06 +00:00
|
|
|
'ENGINE': 'django.db.backends.mysql',
|
|
|
|
'NAME': 'myproject',
|
2014-05-20 19:54:56 +00:00
|
|
|
'HOST': 'dbreplica',
|
2015-09-05 16:57:05 +00:00
|
|
|
'TEST': {
|
|
|
|
'MIRROR': 'default',
|
|
|
|
},
|
2012-12-22 00:59:06 +00:00
|
|
|
# ... plus some other settings
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-20 19:54:56 +00:00
|
|
|
In this setup, we have two database servers: ``dbprimary``, described
|
|
|
|
by the database alias ``default``, and ``dbreplica`` described by the
|
|
|
|
alias ``replica``. As you might expect, ``dbreplica`` has been configured
|
|
|
|
by the database administrator as a read replica of ``dbprimary``, so in
|
|
|
|
normal activity, any write to ``default`` will appear on ``replica``.
|
2012-12-22 00:59:06 +00:00
|
|
|
|
|
|
|
If Django created two independent test databases, this would break any
|
2014-05-20 19:54:56 +00:00
|
|
|
tests that expected replication to occur. However, the ``replica``
|
2012-12-22 00:59:06 +00:00
|
|
|
database has been configured as a test mirror (using the
|
2015-09-05 16:57:05 +00:00
|
|
|
:setting:`MIRROR <TEST_MIRROR>` test setting), indicating that under
|
|
|
|
testing, ``replica`` should be treated as a mirror of ``default``.
|
2012-12-22 00:59:06 +00:00
|
|
|
|
2014-05-20 19:54:56 +00:00
|
|
|
When the test environment is configured, a test version of ``replica``
|
|
|
|
will *not* be created. Instead the connection to ``replica``
|
2012-12-22 00:59:06 +00:00
|
|
|
will be redirected to point at ``default``. As a result, writes to
|
2014-05-20 19:54:56 +00:00
|
|
|
``default`` will appear on ``replica`` -- but because they are actually
|
2012-12-22 00:59:06 +00:00
|
|
|
the same database, not because there is data replication between the
|
|
|
|
two databases.
|
|
|
|
|
|
|
|
.. _topics-testing-creation-dependencies:
|
|
|
|
|
|
|
|
Controlling creation order for test databases
|
|
|
|
---------------------------------------------
|
|
|
|
|
2013-05-31 12:18:29 +00:00
|
|
|
By default, Django will assume all databases depend on the ``default``
|
|
|
|
database and therefore always create the ``default`` database first.
|
2012-12-22 00:59:06 +00:00
|
|
|
However, no guarantees are made on the creation order of any other
|
|
|
|
databases in your test setup.
|
|
|
|
|
|
|
|
If your database configuration requires a specific creation order, you
|
2015-09-05 16:57:05 +00:00
|
|
|
can specify the dependencies that exist using the :setting:`DEPENDENCIES
|
|
|
|
<TEST_DEPENDENCIES>` test setting. Consider the following (simplified)
|
|
|
|
example database configuration::
|
2012-12-22 00:59:06 +00:00
|
|
|
|
|
|
|
DATABASES = {
|
|
|
|
'default': {
|
2015-09-05 16:57:05 +00:00
|
|
|
# ... db settings
|
|
|
|
'TEST': {
|
|
|
|
'DEPENDENCIES': ['diamonds'],
|
|
|
|
},
|
2012-12-22 00:59:06 +00:00
|
|
|
},
|
|
|
|
'diamonds': {
|
2017-05-25 09:44:55 +00:00
|
|
|
# ... db settings
|
2015-09-05 16:57:05 +00:00
|
|
|
'TEST': {
|
|
|
|
'DEPENDENCIES': [],
|
|
|
|
},
|
2012-12-22 00:59:06 +00:00
|
|
|
},
|
|
|
|
'clubs': {
|
|
|
|
# ... db settings
|
2015-09-05 16:57:05 +00:00
|
|
|
'TEST': {
|
|
|
|
'DEPENDENCIES': ['diamonds'],
|
|
|
|
},
|
2012-12-22 00:59:06 +00:00
|
|
|
},
|
|
|
|
'spades': {
|
|
|
|
# ... db settings
|
2015-09-05 16:57:05 +00:00
|
|
|
'TEST': {
|
|
|
|
'DEPENDENCIES': ['diamonds', 'hearts'],
|
|
|
|
},
|
2012-12-22 00:59:06 +00:00
|
|
|
},
|
|
|
|
'hearts': {
|
|
|
|
# ... db settings
|
2015-09-05 16:57:05 +00:00
|
|
|
'TEST': {
|
|
|
|
'DEPENDENCIES': ['diamonds', 'clubs'],
|
|
|
|
},
|
2012-12-22 00:59:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Under this configuration, the ``diamonds`` database will be created first,
|
|
|
|
as it is the only database alias without dependencies. The ``default`` and
|
|
|
|
``clubs`` alias will be created next (although the order of creation of this
|
2015-09-05 16:57:05 +00:00
|
|
|
pair is not guaranteed), then ``hearts``, and finally ``spades``.
|
2012-12-22 00:59:06 +00:00
|
|
|
|
2015-09-05 16:57:05 +00:00
|
|
|
If there are any circular dependencies in the :setting:`DEPENDENCIES
|
|
|
|
<TEST_DEPENDENCIES>` definition, an
|
|
|
|
:exc:`~django.core.exceptions.ImproperlyConfigured` exception will be raised.
|
2012-12-22 00:59:06 +00:00
|
|
|
|
2013-06-11 20:56:09 +00:00
|
|
|
Advanced features of ``TransactionTestCase``
|
|
|
|
============================================
|
|
|
|
|
|
|
|
.. attribute:: TransactionTestCase.available_apps
|
|
|
|
|
|
|
|
.. warning::
|
|
|
|
|
|
|
|
This attribute is a private API. It may be changed or removed without
|
2013-07-28 01:45:25 +00:00
|
|
|
a deprecation period in the future, for instance to accommodate changes
|
2013-06-11 20:56:09 +00:00
|
|
|
in application loading.
|
|
|
|
|
|
|
|
It's used to optimize Django's own test suite, which contains hundreds
|
|
|
|
of models but no relations between models in different applications.
|
|
|
|
|
|
|
|
By default, ``available_apps`` is set to ``None``. After each test, Django
|
|
|
|
calls :djadmin:`flush` to reset the database state. This empties all tables
|
2013-07-30 10:52:52 +00:00
|
|
|
and emits the :data:`~django.db.models.signals.post_migrate` signal, which
|
2018-09-26 19:06:43 +00:00
|
|
|
recreates one content type and four permissions for each model. This
|
2013-06-11 20:56:09 +00:00
|
|
|
operation gets expensive proportionally to the number of models.
|
|
|
|
|
|
|
|
Setting ``available_apps`` to a list of applications instructs Django to
|
|
|
|
behave as if only the models from these applications were available. The
|
|
|
|
behavior of ``TransactionTestCase`` changes as follows:
|
|
|
|
|
2013-07-30 10:52:52 +00:00
|
|
|
- :data:`~django.db.models.signals.post_migrate` is fired before each
|
2013-06-11 20:56:09 +00:00
|
|
|
test to create the content types and permissions for each model in
|
|
|
|
available apps, in case they're missing.
|
|
|
|
- After each test, Django empties only tables corresponding to models in
|
|
|
|
available apps. However, at the database level, truncation may cascade to
|
|
|
|
related models in unavailable apps. Furthermore
|
2013-07-30 10:52:52 +00:00
|
|
|
:data:`~django.db.models.signals.post_migrate` isn't fired; it will be
|
2013-06-11 20:56:09 +00:00
|
|
|
fired by the next ``TransactionTestCase``, after the correct set of
|
|
|
|
applications is selected.
|
|
|
|
|
|
|
|
Since the database isn't fully flushed, if a test creates instances of
|
|
|
|
models not included in ``available_apps``, they will leak and they may
|
|
|
|
cause unrelated tests to fail. Be careful with tests that use sessions;
|
|
|
|
the default session engine stores them in the database.
|
|
|
|
|
2013-07-30 10:52:52 +00:00
|
|
|
Since :data:`~django.db.models.signals.post_migrate` isn't emitted after
|
2013-06-11 20:56:09 +00:00
|
|
|
flushing the database, its state after a ``TransactionTestCase`` isn't the
|
|
|
|
same as after a ``TestCase``: it's missing the rows created by listeners
|
2013-07-30 10:52:52 +00:00
|
|
|
to :data:`~django.db.models.signals.post_migrate`. Considering the
|
2013-06-11 20:56:09 +00:00
|
|
|
:ref:`order in which tests are executed <order-of-tests>`, this isn't an
|
|
|
|
issue, provided either all ``TransactionTestCase`` in a given test suite
|
|
|
|
declare ``available_apps``, or none of them.
|
|
|
|
|
|
|
|
``available_apps`` is mandatory in Django's own test suite.
|
|
|
|
|
|
|
|
.. attribute:: TransactionTestCase.reset_sequences
|
|
|
|
|
|
|
|
Setting ``reset_sequences = True`` on a ``TransactionTestCase`` will make
|
|
|
|
sure sequences are always reset before the test run::
|
|
|
|
|
|
|
|
class TestsThatDependsOnPrimaryKeySequences(TransactionTestCase):
|
|
|
|
reset_sequences = True
|
|
|
|
|
|
|
|
def test_animal_pk(self):
|
|
|
|
lion = Animal.objects.create(name="lion", sound="roar")
|
|
|
|
# lion.pk is guaranteed to always be 1
|
|
|
|
self.assertEqual(lion.pk, 1)
|
|
|
|
|
|
|
|
Unless you are explicitly testing primary keys sequence numbers, it is
|
|
|
|
recommended that you do not hard code primary key values in tests.
|
|
|
|
|
|
|
|
Using ``reset_sequences = True`` will slow down the test, since the primary
|
2017-10-31 01:47:09 +00:00
|
|
|
key reset is a relatively expensive database operation.
|
2013-06-11 20:56:09 +00:00
|
|
|
|
2019-06-05 14:35:36 +00:00
|
|
|
.. _topics-testing-enforce-run-sequentially:
|
|
|
|
|
|
|
|
Enforce running test classes sequentially
|
|
|
|
=========================================
|
|
|
|
|
|
|
|
If you have test classes that cannot be run in parallel (e.g. because they
|
|
|
|
share a common resource), you can use ``django.test.testcases.SerializeMixin``
|
|
|
|
to run them sequentially. This mixin uses a filesystem ``lockfile``.
|
|
|
|
|
|
|
|
For example, you can use ``__file__`` to determine that all test classes in the
|
|
|
|
same file that inherit from ``SerializeMixin`` will run sequentially::
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
|
|
|
from django.test import TestCase
|
|
|
|
from django.test.testcases import SerializeMixin
|
|
|
|
|
|
|
|
class ImageTestCaseMixin(SerializeMixin):
|
|
|
|
lockfile = __file__
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.filename = os.path.join(temp_storage_dir, 'my_file.png')
|
|
|
|
self.file = create_file(self.filename)
|
|
|
|
|
|
|
|
class RemoveImageTests(ImageTestCaseMixin, TestCase):
|
|
|
|
def test_remove_image(self):
|
|
|
|
os.remove(self.filename)
|
|
|
|
self.assertFalse(os.path.exists(self.filename))
|
|
|
|
|
|
|
|
class ResizeImageTests(ImageTestCaseMixin, TestCase):
|
|
|
|
def test_resize_image(self):
|
|
|
|
resize_image(self.file, (48, 48))
|
|
|
|
self.assertEqual(get_image_size(self.file), (48, 48))
|
|
|
|
|
2015-09-24 18:48:49 +00:00
|
|
|
.. _testing-reusable-applications:
|
2012-12-22 00:59:06 +00:00
|
|
|
|
2014-11-21 19:53:36 +00:00
|
|
|
Using the Django test runner to test reusable applications
|
|
|
|
==========================================================
|
2012-12-22 00:59:06 +00:00
|
|
|
|
2014-11-21 19:53:36 +00:00
|
|
|
If you are writing a :doc:`reusable application </intro/reusable-apps>`
|
|
|
|
you may want to use the Django test runner to run your own test suite
|
|
|
|
and thus benefit from the Django testing infrastructure.
|
2012-12-22 00:59:06 +00:00
|
|
|
|
2014-11-21 19:53:36 +00:00
|
|
|
A common practice is a *tests* directory next to the application code, with the
|
|
|
|
following structure::
|
2012-12-22 00:59:06 +00:00
|
|
|
|
2014-11-21 19:53:36 +00:00
|
|
|
runtests.py
|
|
|
|
polls/
|
|
|
|
__init__.py
|
|
|
|
models.py
|
|
|
|
...
|
|
|
|
tests/
|
|
|
|
__init__.py
|
|
|
|
models.py
|
|
|
|
test_settings.py
|
|
|
|
tests.py
|
2012-12-22 00:59:06 +00:00
|
|
|
|
2014-11-21 19:53:36 +00:00
|
|
|
Let's take a look inside a couple of those files:
|
|
|
|
|
2018-09-10 17:00:34 +00:00
|
|
|
.. code-block:: python
|
|
|
|
:caption: runtests.py
|
2014-11-21 19:53:36 +00:00
|
|
|
|
2017-03-07 15:10:32 +00:00
|
|
|
#!/usr/bin/env python
|
2014-11-21 19:53:36 +00:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
|
|
|
import django
|
|
|
|
from django.conf import settings
|
|
|
|
from django.test.utils import get_runner
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
os.environ['DJANGO_SETTINGS_MODULE'] = 'tests.test_settings'
|
|
|
|
django.setup()
|
|
|
|
TestRunner = get_runner(settings)
|
|
|
|
test_runner = TestRunner()
|
|
|
|
failures = test_runner.run_tests(["tests"])
|
|
|
|
sys.exit(bool(failures))
|
|
|
|
|
|
|
|
|
|
|
|
This is the script that you invoke to run the test suite. It sets up the
|
|
|
|
Django environment, creates the test database and runs the tests.
|
|
|
|
|
|
|
|
For the sake of clarity, this example contains only the bare minimum
|
|
|
|
necessary to use the Django test runner. You may want to add
|
|
|
|
command-line options for controlling verbosity, passing in specific test
|
|
|
|
labels to run, etc.
|
|
|
|
|
2018-09-10 17:00:34 +00:00
|
|
|
.. code-block:: python
|
|
|
|
:caption: tests/test_settings.py
|
2014-11-21 19:53:36 +00:00
|
|
|
|
|
|
|
SECRET_KEY = 'fake-key'
|
|
|
|
INSTALLED_APPS = [
|
|
|
|
"tests",
|
|
|
|
]
|
|
|
|
|
|
|
|
This file contains the :doc:`Django settings </topics/settings>`
|
|
|
|
required to run your app's tests.
|
|
|
|
|
|
|
|
Again, this is a minimal example; your tests may require additional
|
|
|
|
settings to run.
|
|
|
|
|
|
|
|
Since the *tests* package is included in :setting:`INSTALLED_APPS` when
|
|
|
|
running your tests, you can define test-only models in its ``models.py``
|
|
|
|
file.
|
2014-04-28 16:43:33 +00:00
|
|
|
|
|
|
|
|
2012-12-22 00:59:06 +00:00
|
|
|
.. _other-testing-frameworks:
|
|
|
|
|
|
|
|
Using different testing frameworks
|
|
|
|
==================================
|
|
|
|
|
2013-05-11 03:08:45 +00:00
|
|
|
Clearly, :mod:`unittest` is not the only Python testing framework. While Django
|
|
|
|
doesn't provide explicit support for alternative frameworks, it does provide a
|
|
|
|
way to invoke tests constructed for an alternative framework as if they were
|
|
|
|
normal Django tests.
|
2012-12-22 00:59:06 +00:00
|
|
|
|
|
|
|
When you run ``./manage.py test``, Django looks at the :setting:`TEST_RUNNER`
|
|
|
|
setting to determine what to do. By default, :setting:`TEST_RUNNER` points to
|
2013-05-11 03:08:45 +00:00
|
|
|
``'django.test.runner.DiscoverRunner'``. This class defines the default Django
|
2012-12-22 00:59:06 +00:00
|
|
|
testing behavior. This behavior involves:
|
|
|
|
|
|
|
|
#. Performing global pre-test setup.
|
|
|
|
|
2013-05-11 03:08:45 +00:00
|
|
|
#. Looking for tests in any file below the current directory whose name matches
|
|
|
|
the pattern ``test*.py``.
|
2012-12-22 00:59:06 +00:00
|
|
|
|
|
|
|
#. Creating the test databases.
|
|
|
|
|
2013-07-25 15:19:36 +00:00
|
|
|
#. Running ``migrate`` to install models and initial data into the test
|
2012-12-22 00:59:06 +00:00
|
|
|
databases.
|
|
|
|
|
2016-03-18 14:24:13 +00:00
|
|
|
#. Running the :doc:`system checks </topics/checks>`.
|
|
|
|
|
2013-05-11 03:08:45 +00:00
|
|
|
#. Running the tests that were found.
|
2012-12-22 00:59:06 +00:00
|
|
|
|
|
|
|
#. Destroying the test databases.
|
|
|
|
|
|
|
|
#. Performing global post-test teardown.
|
|
|
|
|
|
|
|
If you define your own test runner class and point :setting:`TEST_RUNNER` at
|
|
|
|
that class, Django will execute your test runner whenever you run
|
|
|
|
``./manage.py test``. In this way, it is possible to use any test framework
|
|
|
|
that can be executed from Python code, or to modify the Django test execution
|
|
|
|
process to satisfy whatever testing requirements you may have.
|
|
|
|
|
|
|
|
.. _topics-testing-test_runner:
|
|
|
|
|
|
|
|
Defining a test runner
|
|
|
|
----------------------
|
|
|
|
|
2013-05-11 03:08:45 +00:00
|
|
|
.. currentmodule:: django.test.runner
|
2012-12-22 00:59:06 +00:00
|
|
|
|
|
|
|
A test runner is a class defining a ``run_tests()`` method. Django ships
|
2013-09-07 20:10:07 +00:00
|
|
|
with a ``DiscoverRunner`` class that defines the default Django testing
|
|
|
|
behavior. This class defines the ``run_tests()`` entry point, plus a
|
|
|
|
selection of other methods that are used to by ``run_tests()`` to set up,
|
|
|
|
execute and tear down the test suite.
|
2012-12-22 00:59:06 +00:00
|
|
|
|
2020-07-10 07:20:18 +00:00
|
|
|
.. class:: DiscoverRunner(pattern='test*.py', top_level=None, verbosity=1, interactive=True, failfast=False, keepdb=False, reverse=False, debug_mode=False, debug_sql=False, test_name_patterns=None, pdb=False, **kwargs)
|
2013-05-11 03:08:45 +00:00
|
|
|
|
|
|
|
``DiscoverRunner`` will search for tests in any file matching ``pattern``.
|
|
|
|
|
|
|
|
``top_level`` can be used to specify the directory containing your
|
|
|
|
top-level Python modules. Usually Django can figure this out automatically,
|
|
|
|
so it's not necessary to specify this option. If specified, it should
|
|
|
|
generally be the directory containing your ``manage.py`` file.
|
2012-12-22 00:59:06 +00:00
|
|
|
|
|
|
|
``verbosity`` determines the amount of notification and debug information
|
|
|
|
that will be printed to the console; ``0`` is no output, ``1`` is normal
|
|
|
|
output, and ``2`` is verbose output.
|
|
|
|
|
|
|
|
If ``interactive`` is ``True``, the test suite has permission to ask the
|
|
|
|
user for instructions when the test suite is executed. An example of this
|
|
|
|
behavior would be asking for permission to delete an existing test
|
|
|
|
database. If ``interactive`` is ``False``, the test suite must be able to
|
|
|
|
run without any manual intervention.
|
|
|
|
|
|
|
|
If ``failfast`` is ``True``, the test suite will stop running after the
|
|
|
|
first test failure is detected.
|
|
|
|
|
2014-11-04 19:26:37 +00:00
|
|
|
If ``keepdb`` is ``True``, the test suite will use the existing database,
|
|
|
|
or create one if necessary. If ``False``, a new database will be created,
|
|
|
|
prompting the user to remove the existing one, if present.
|
|
|
|
|
2014-11-22 16:59:05 +00:00
|
|
|
If ``reverse`` is ``True``, test cases will be executed in the opposite
|
|
|
|
order. This could be useful to debug tests that aren't properly isolated
|
|
|
|
and have side effects. :ref:`Grouping by test class <order-of-tests>` is
|
|
|
|
preserved when using this option.
|
|
|
|
|
2016-08-09 08:40:40 +00:00
|
|
|
``debug_mode`` specifies what the :setting:`DEBUG` setting should be
|
|
|
|
set to prior to running tests.
|
|
|
|
|
2015-01-10 22:52:59 +00:00
|
|
|
If ``debug_sql`` is ``True``, failing test cases will output SQL queries
|
|
|
|
logged to the :ref:`django.db.backends logger <django-db-logger>` as well
|
|
|
|
as the traceback. If ``verbosity`` is ``2``, then queries in all tests are
|
|
|
|
output.
|
|
|
|
|
2019-03-07 20:58:30 +00:00
|
|
|
``test_name_patterns`` can be used to specify a set of patterns for
|
|
|
|
filtering test methods and classes by their names.
|
|
|
|
|
2020-07-10 07:20:18 +00:00
|
|
|
If ``pdb`` is ``True``, a debugger (``pdb`` or ``ipdb``) will be spawned at
|
|
|
|
each test error or failure.
|
|
|
|
|
2013-05-11 03:08:45 +00:00
|
|
|
Django may, from time to time, extend the capabilities of the test runner
|
|
|
|
by adding new arguments. The ``**kwargs`` declaration allows for this
|
|
|
|
expansion. If you subclass ``DiscoverRunner`` or write your own test
|
|
|
|
runner, ensure it accepts ``**kwargs``.
|
2012-12-22 00:59:06 +00:00
|
|
|
|
|
|
|
Your test runner may also define additional command-line options.
|
2014-06-06 18:12:23 +00:00
|
|
|
Create or override an ``add_arguments(cls, parser)`` class method and add
|
|
|
|
custom arguments by calling ``parser.add_argument()`` inside the method, so
|
|
|
|
that the :djadmin:`test` command will be able to use those arguments.
|
|
|
|
|
2012-12-22 00:59:06 +00:00
|
|
|
Attributes
|
|
|
|
~~~~~~~~~~
|
|
|
|
|
2013-09-10 13:49:39 +00:00
|
|
|
.. attribute:: DiscoverRunner.test_suite
|
|
|
|
|
|
|
|
The class used to build the test suite. By default it is set to
|
|
|
|
``unittest.TestSuite``. This can be overridden if you wish to implement
|
|
|
|
different logic for collecting tests.
|
|
|
|
|
|
|
|
.. attribute:: DiscoverRunner.test_runner
|
|
|
|
|
|
|
|
This is the class of the low-level test runner which is used to execute
|
|
|
|
the individual tests and format the results. By default it is set to
|
|
|
|
``unittest.TextTestRunner``. Despite the unfortunate similarity in
|
|
|
|
naming conventions, this is not the same type of class as
|
2014-03-01 02:03:46 +00:00
|
|
|
``DiscoverRunner``, which covers a broader set of responsibilities. You
|
2013-09-10 13:49:39 +00:00
|
|
|
can override this attribute to modify the way tests are run and reported.
|
|
|
|
|
2013-09-07 20:10:07 +00:00
|
|
|
.. attribute:: DiscoverRunner.test_loader
|
|
|
|
|
|
|
|
This is the class that loads tests, whether from TestCases or modules or
|
|
|
|
otherwise and bundles them into test suites for the runner to execute.
|
|
|
|
By default it is set to ``unittest.defaultTestLoader``. You can override
|
|
|
|
this attribute if your tests are going to be loaded in unusual ways.
|
|
|
|
|
2012-12-22 00:59:06 +00:00
|
|
|
Methods
|
|
|
|
~~~~~~~
|
|
|
|
|
2013-05-11 03:08:45 +00:00
|
|
|
.. method:: DiscoverRunner.run_tests(test_labels, extra_tests=None, **kwargs)
|
2012-12-22 00:59:06 +00:00
|
|
|
|
|
|
|
Run the test suite.
|
|
|
|
|
2013-12-16 21:47:11 +00:00
|
|
|
``test_labels`` allows you to specify which tests to run and supports
|
|
|
|
several formats (see :meth:`DiscoverRunner.build_suite` for a list of
|
|
|
|
supported formats).
|
2012-12-22 00:59:06 +00:00
|
|
|
|
|
|
|
``extra_tests`` is a list of extra ``TestCase`` instances to add to the
|
|
|
|
suite that is executed by the test runner. These extra tests are run
|
|
|
|
in addition to those discovered in the modules listed in ``test_labels``.
|
|
|
|
|
|
|
|
This method should return the number of tests that failed.
|
|
|
|
|
2014-06-06 18:12:23 +00:00
|
|
|
.. classmethod:: DiscoverRunner.add_arguments(parser)
|
|
|
|
|
|
|
|
Override this class method to add custom arguments accepted by the
|
|
|
|
:djadmin:`test` management command. See
|
|
|
|
:py:meth:`argparse.ArgumentParser.add_argument()` for details about adding
|
|
|
|
arguments to a parser.
|
|
|
|
|
2013-05-11 03:08:45 +00:00
|
|
|
.. method:: DiscoverRunner.setup_test_environment(**kwargs)
|
2012-12-22 00:59:06 +00:00
|
|
|
|
2013-05-06 17:55:02 +00:00
|
|
|
Sets up the test environment by calling
|
|
|
|
:func:`~django.test.utils.setup_test_environment` and setting
|
2016-08-09 08:40:40 +00:00
|
|
|
:setting:`DEBUG` to ``self.debug_mode`` (defaults to ``False``).
|
2012-12-22 00:59:06 +00:00
|
|
|
|
2013-05-11 03:08:45 +00:00
|
|
|
.. method:: DiscoverRunner.build_suite(test_labels, extra_tests=None, **kwargs)
|
2012-12-22 00:59:06 +00:00
|
|
|
|
|
|
|
Constructs a test suite that matches the test labels provided.
|
|
|
|
|
|
|
|
``test_labels`` is a list of strings describing the tests to be run. A test
|
2013-12-16 21:47:11 +00:00
|
|
|
label can take one of four forms:
|
2012-12-22 00:59:06 +00:00
|
|
|
|
2013-12-16 21:47:11 +00:00
|
|
|
* ``path.to.test_module.TestCase.test_method`` -- Run a single test method
|
|
|
|
in a test case.
|
|
|
|
* ``path.to.test_module.TestCase`` -- Run all the test methods in a test
|
2012-12-22 00:59:06 +00:00
|
|
|
case.
|
2013-12-16 21:47:11 +00:00
|
|
|
* ``path.to.module`` -- Search for and run all tests in the named Python
|
|
|
|
package or module.
|
|
|
|
* ``path/to/directory`` -- Search for and run all tests below the named
|
|
|
|
directory.
|
2012-12-22 00:59:06 +00:00
|
|
|
|
2013-12-16 21:47:11 +00:00
|
|
|
If ``test_labels`` has a value of ``None``, the test runner will search for
|
|
|
|
tests in all files below the current directory whose names match its
|
|
|
|
``pattern`` (see above).
|
2012-12-22 00:59:06 +00:00
|
|
|
|
|
|
|
``extra_tests`` is a list of extra ``TestCase`` instances to add to the
|
|
|
|
suite that is executed by the test runner. These extra tests are run
|
|
|
|
in addition to those discovered in the modules listed in ``test_labels``.
|
|
|
|
|
|
|
|
Returns a ``TestSuite`` instance ready to be run.
|
|
|
|
|
2013-05-11 03:08:45 +00:00
|
|
|
.. method:: DiscoverRunner.setup_databases(**kwargs)
|
2012-12-22 00:59:06 +00:00
|
|
|
|
2016-07-02 22:20:14 +00:00
|
|
|
Creates the test databases by calling
|
|
|
|
:func:`~django.test.utils.setup_databases`.
|
2012-12-22 00:59:06 +00:00
|
|
|
|
2020-02-07 09:37:25 +00:00
|
|
|
.. method:: DiscoverRunner.run_checks(databases)
|
2016-03-18 14:24:13 +00:00
|
|
|
|
2020-02-07 09:37:25 +00:00
|
|
|
Runs the :doc:`system checks </topics/checks>` on the test ``databases``.
|
|
|
|
|
|
|
|
.. versionadded:: 3.1
|
|
|
|
|
|
|
|
The ``databases`` parameter was added.
|
2016-03-18 14:24:13 +00:00
|
|
|
|
2013-05-11 03:08:45 +00:00
|
|
|
.. method:: DiscoverRunner.run_suite(suite, **kwargs)
|
2012-12-22 00:59:06 +00:00
|
|
|
|
|
|
|
Runs the test suite.
|
|
|
|
|
|
|
|
Returns the result produced by the running the test suite.
|
|
|
|
|
2016-08-04 17:26:21 +00:00
|
|
|
.. method:: DiscoverRunner.get_test_runner_kwargs()
|
|
|
|
|
|
|
|
Returns the keyword arguments to instantiate the
|
|
|
|
``DiscoverRunner.test_runner`` with.
|
|
|
|
|
2013-05-11 03:08:45 +00:00
|
|
|
.. method:: DiscoverRunner.teardown_databases(old_config, **kwargs)
|
2012-12-22 00:59:06 +00:00
|
|
|
|
2016-07-02 22:20:14 +00:00
|
|
|
Destroys the test databases, restoring pre-test conditions by calling
|
|
|
|
:func:`~django.test.utils.teardown_databases`.
|
2012-12-22 00:59:06 +00:00
|
|
|
|
2013-05-11 03:08:45 +00:00
|
|
|
.. method:: DiscoverRunner.teardown_test_environment(**kwargs)
|
2012-12-22 00:59:06 +00:00
|
|
|
|
|
|
|
Restores the pre-test environment.
|
|
|
|
|
2013-05-11 03:08:45 +00:00
|
|
|
.. method:: DiscoverRunner.suite_result(suite, result, **kwargs)
|
2012-12-22 00:59:06 +00:00
|
|
|
|
|
|
|
Computes and returns a return code based on a test suite, and the result
|
|
|
|
from that test suite.
|
|
|
|
|
|
|
|
|
|
|
|
Testing utilities
|
|
|
|
-----------------
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``django.test.utils``
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~
|
2013-05-06 17:45:24 +00:00
|
|
|
|
2012-12-22 00:59:06 +00:00
|
|
|
.. module:: django.test.utils
|
|
|
|
:synopsis: Helpers to write custom test runners.
|
|
|
|
|
|
|
|
To assist in the creation of your own test runner, Django provides a number of
|
|
|
|
utility methods in the ``django.test.utils`` module.
|
|
|
|
|
2016-08-08 10:04:27 +00:00
|
|
|
.. function:: setup_test_environment(debug=None)
|
2012-12-22 00:59:06 +00:00
|
|
|
|
2016-08-04 18:29:59 +00:00
|
|
|
Performs global pre-test setup, such as installing instrumentation for the
|
|
|
|
template rendering system and setting up the dummy email outbox.
|
2012-12-22 00:59:06 +00:00
|
|
|
|
2016-08-08 10:04:27 +00:00
|
|
|
If ``debug`` isn't ``None``, the :setting:`DEBUG` setting is updated to its
|
|
|
|
value.
|
|
|
|
|
2012-12-22 00:59:06 +00:00
|
|
|
.. function:: teardown_test_environment()
|
|
|
|
|
2016-08-04 18:29:59 +00:00
|
|
|
Performs global post-test teardown, such as removing instrumentation from
|
|
|
|
the template system and restoring normal email services.
|
2012-12-22 00:59:06 +00:00
|
|
|
|
2018-07-12 04:14:24 +00:00
|
|
|
.. function:: setup_databases(verbosity, interactive, keepdb=False, debug_sql=False, parallel=0, aliases=None, **kwargs)
|
2016-07-02 22:20:14 +00:00
|
|
|
|
|
|
|
Creates the test databases.
|
|
|
|
|
|
|
|
Returns a data structure that provides enough detail to undo the changes
|
|
|
|
that have been made. This data will be provided to the
|
|
|
|
:func:`teardown_databases` function at the conclusion of testing.
|
|
|
|
|
2018-07-12 04:14:24 +00:00
|
|
|
The ``aliases`` argument determines which :setting:`DATABASES` aliases test
|
|
|
|
databases should be setup for. If it's not provided, it defaults to all of
|
|
|
|
:setting:`DATABASES` aliases.
|
|
|
|
|
2016-07-02 22:20:14 +00:00
|
|
|
.. function:: teardown_databases(old_config, parallel=0, keepdb=False)
|
|
|
|
|
|
|
|
Destroys the test databases, restoring pre-test conditions.
|
|
|
|
|
|
|
|
``old_config`` is a data structure defining the changes in the database
|
|
|
|
configuration that need to be reversed. It's the return value of the
|
|
|
|
:meth:`setup_databases` method.
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``django.db.connection.creation``
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2013-05-06 17:45:24 +00:00
|
|
|
|
2012-12-22 00:59:06 +00:00
|
|
|
.. currentmodule:: django.db.connection.creation
|
|
|
|
|
2013-05-06 17:45:24 +00:00
|
|
|
The creation module of the database backend also provides some utilities that
|
|
|
|
can be useful during testing.
|
2012-12-22 00:59:06 +00:00
|
|
|
|
2015-07-27 12:35:21 +00:00
|
|
|
.. function:: create_test_db(verbosity=1, autoclobber=False, serialize=True, keepdb=False)
|
2012-12-22 00:59:06 +00:00
|
|
|
|
2013-07-25 15:19:36 +00:00
|
|
|
Creates a new test database and runs ``migrate`` against it.
|
2012-12-22 00:59:06 +00:00
|
|
|
|
|
|
|
``verbosity`` has the same behavior as in ``run_tests()``.
|
|
|
|
|
|
|
|
``autoclobber`` describes the behavior that will occur if a
|
|
|
|
database with the same name as the test database is discovered:
|
|
|
|
|
|
|
|
* If ``autoclobber`` is ``False``, the user will be asked to
|
|
|
|
approve destroying the existing database. ``sys.exit`` is
|
|
|
|
called if the user does not approve.
|
|
|
|
|
|
|
|
* If autoclobber is ``True``, the database will be destroyed
|
|
|
|
without consulting the user.
|
|
|
|
|
2014-06-09 02:30:15 +00:00
|
|
|
``serialize`` determines if Django serializes the database into an
|
|
|
|
in-memory JSON string before running tests (used to restore the database
|
|
|
|
state between tests if you don't have transactions). You can set this to
|
2014-09-15 15:17:12 +00:00
|
|
|
``False`` to speed up creation time if you don't have any test classes
|
|
|
|
with :ref:`serialized_rollback=True <test-case-serialized-rollback>`.
|
|
|
|
|
2015-01-26 20:39:52 +00:00
|
|
|
If you are using the default test runner, you can control this with the
|
|
|
|
the :setting:`SERIALIZE <TEST_SERIALIZE>` entry in the :setting:`TEST
|
|
|
|
<DATABASE-TEST>` dictionary.
|
2014-06-09 02:30:15 +00:00
|
|
|
|
2014-06-09 15:12:28 +00:00
|
|
|
``keepdb`` determines if the test run should use an existing
|
|
|
|
database, or create a new one. If ``True``, the existing
|
|
|
|
database will be used, or created if not present. If ``False``,
|
|
|
|
a new database will be created, prompting the user to remove
|
|
|
|
the existing one, if present.
|
|
|
|
|
2012-12-22 00:59:06 +00:00
|
|
|
Returns the name of the test database that it created.
|
|
|
|
|
|
|
|
``create_test_db()`` has the side effect of modifying the value of
|
|
|
|
:setting:`NAME` in :setting:`DATABASES` to match the name of the test
|
|
|
|
database.
|
|
|
|
|
2015-07-27 12:35:21 +00:00
|
|
|
.. function:: destroy_test_db(old_database_name, verbosity=1, keepdb=False)
|
2012-12-22 00:59:06 +00:00
|
|
|
|
|
|
|
Destroys the database whose name is the value of :setting:`NAME` in
|
|
|
|
:setting:`DATABASES`, and sets :setting:`NAME` to the value of
|
|
|
|
``old_database_name``.
|
|
|
|
|
|
|
|
The ``verbosity`` argument has the same behavior as for
|
2013-05-11 03:08:45 +00:00
|
|
|
:class:`~django.test.runner.DiscoverRunner`.
|
2012-12-22 00:59:06 +00:00
|
|
|
|
2014-06-05 21:42:08 +00:00
|
|
|
If the ``keepdb`` argument is ``True``, then the connection to the
|
|
|
|
database will be closed, but the database will not be destroyed.
|
|
|
|
|
2012-12-22 00:59:06 +00:00
|
|
|
.. _topics-testing-code-coverage:
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
Integration with ``coverage.py``
|
|
|
|
================================
|
2012-12-22 00:59:06 +00:00
|
|
|
|
|
|
|
Code coverage describes how much source code has been tested. It shows which
|
|
|
|
parts of your code are being exercised by tests and which are not. It's an
|
|
|
|
important part of testing applications, so it's strongly recommended to check
|
|
|
|
the coverage of your tests.
|
|
|
|
|
|
|
|
Django can be easily integrated with `coverage.py`_, a tool for measuring code
|
|
|
|
coverage of Python programs. First, `install coverage.py`_. Next, run the
|
|
|
|
following from your project folder containing ``manage.py``::
|
|
|
|
|
|
|
|
coverage run --source='.' manage.py test myapp
|
|
|
|
|
|
|
|
This runs your tests and collects coverage data of the executed files in your
|
|
|
|
project. You can see a report of this data by typing following command::
|
|
|
|
|
|
|
|
coverage report
|
|
|
|
|
|
|
|
Note that some Django code was executed while running tests, but it is not
|
|
|
|
listed here because of the ``source`` flag passed to the previous command.
|
|
|
|
|
|
|
|
For more options like annotated HTML listings detailing missed lines, see the
|
|
|
|
`coverage.py`_ docs.
|
|
|
|
|
2018-01-07 13:28:41 +00:00
|
|
|
.. _coverage.py: https://coverage.readthedocs.io/
|
2018-04-17 22:19:29 +00:00
|
|
|
.. _install coverage.py: https://pypi.org/project/coverage/
|