Kaydet (Commit) d19109fd authored tarafından Ramiro Morales's avatar Ramiro Morales

Fixed #19497 -- Refactored testing docs.

Thanks Tim Graham for the review and suggestions.
üst 52a2588d
......@@ -180,7 +180,11 @@ testing of Django applications:
:doc:`Overview <ref/django-admin>` |
:doc:`Adding custom commands <howto/custom-management-commands>`
* **Testing:** :doc:`Overview <topics/testing>`
* **Testing:**
:doc:`Overview <topics/testing/index>` |
:doc:`Writing and running tests <topics/testing/overview>` |
:doc:`Advanced topics <topics/testing/advanced>` |
:doc:`Doctests <topics/testing/doctests>`
* **Deployment:**
:doc:`Overview <howto/deployment/index>` |
......
......@@ -15,8 +15,8 @@ The tests cover:
We appreciate any and all contributions to the test suite!
The Django tests all use the testing infrastructure that ships with Django for
testing applications. See :doc:`Testing Django applications </topics/testing>`
for an explanation of how to write new tests.
testing applications. See :doc:`Testing Django applications
</topics/testing/overview>` for an explanation of how to write new tests.
.. _running-unit-tests:
......
......@@ -281,7 +281,7 @@ correctly in a couple different situations.
computer programming, so there's lots of information out there:
* A good first look at writing tests for Django can be found in the
documentation on :doc:`Testing Django applications</topics/testing/>`.
documentation on :doc:`Testing Django applications </topics/testing/overview>`.
* Dive Into Python (a free online book for beginning Python developers)
includes a great `introduction to Unit Testing`__.
* After reading those, if you want something a little meatier to sink
......
......@@ -632,7 +632,7 @@ a piece of code, it usually means that code should be refactored or removed.
Coverage will help to identify dead code. See
:ref:`topics-testing-code-coverage` for details.
:doc:`Testing Django applications </topics/testing>` has comprehensive
:doc:`Testing Django applications </topics/testing/index>` has comprehensive
information about testing.
.. _Selenium: http://seleniumhq.org/
......
......@@ -71,7 +71,7 @@ of 1.0. This includes these APIs:
external template tags. Before adding any such tags, we'll ensure that
Django raises an error if it tries to load tags with duplicate names.
- :doc:`Testing </topics/testing>`
- :doc:`Testing </topics/testing/index>`
- :doc:`django-admin utility </ref/django-admin>`.
......
......@@ -1036,7 +1036,7 @@ test <app or test identifier>
.. django-admin:: test
Runs tests for all installed models. See :doc:`/topics/testing` for more
Runs tests for all installed models. See :doc:`/topics/testing/index` for more
information.
.. django-admin-option:: --failfast
......@@ -1072,7 +1072,7 @@ For example, this command::
...would perform the following steps:
1. Create a test database, as described in :doc:`/topics/testing`.
1. Create a test database, as described in :ref:`the-test-database`.
2. Populate the test database with fixture data from the given fixtures.
(For more on fixtures, see the documentation for ``loaddata`` above.)
3. Runs the Django development server (as in ``runserver``), pointed at
......@@ -1080,7 +1080,7 @@ For example, this command::
This is useful in a number of ways:
* When you're writing :doc:`unit tests </topics/testing>` of how your views
* When you're writing :doc:`unit tests </topics/testing/overview>` of how your views
act with certain fixture data, you can use ``testserver`` to interact with
the views in a Web browser, manually.
......
......@@ -562,7 +562,7 @@ If the default value (``None``) is used with the SQLite database engine, the
tests will use a memory resident database. For all other database engines the
test database will use the name ``'test_' + DATABASE_NAME``.
See :doc:`/topics/testing`.
See :ref:`the-test-database`.
.. setting:: TEST_CREATE
......@@ -1982,9 +1982,7 @@ TEST_RUNNER
Default: ``'django.test.simple.DjangoTestSuiteRunner'``
The name of the class to use for starting the test suite. See
:doc:`/topics/testing`.
.. _Testing Django Applications: ../testing/
:ref:`other-testing-frameworks`.
.. setting:: THOUSAND_SEPARATOR
......
......@@ -476,7 +476,7 @@ Test signals
.. module:: django.test.signals
:synopsis: Signals sent during testing.
Signals only sent when :doc:`running tests </topics/testing>`.
Signals only sent when :ref:`running tests <running-tests>`.
setting_changed
---------------
......
......@@ -220,7 +220,7 @@ supported :doc:`serialization formats </topics/serialization>`, that will be
loaded into your database at the start of your tests. This makes testing with
real data much easier.
See :doc:`the testing documentation </topics/testing>` for the full details.
See :doc:`the testing documentation </topics/testing/index>` for the full details.
Improvements to the admin interface
-----------------------------------
......
......@@ -51,7 +51,7 @@ Performance improvements
.. currentmodule:: django.test
Tests written using Django's :doc:`testing framework </topics/testing>` now run
Tests written using Django's :doc:`testing framework </topics/testing/index>` now run
dramatically faster (as much as 10 times faster in many cases).
This was accomplished through the introduction of transaction-based tests: when
......
......@@ -102,7 +102,7 @@ Testing improvements
.. currentmodule:: django.test.client
A couple of small but very useful improvements have been made to the
:doc:`testing framework </topics/testing>`:
:doc:`testing framework </topics/testing/index>`:
* The test :class:`Client` now can automatically follow redirects with the
``follow`` argument to :meth:`Client.get` and :meth:`Client.post`. This
......
......@@ -264,14 +264,14 @@ Testing improvements
--------------------
A few notable improvements have been made to the :doc:`testing framework
</topics/testing>`.
</topics/testing/index>`.
Test performance improvements
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.. currentmodule:: django.test
Tests written using Django's :doc:`testing framework </topics/testing>` now run
Tests written using Django's :doc:`testing framework </topics/testing/index>` now run
dramatically faster (as much as 10 times faster in many cases).
This was accomplished through the introduction of transaction-based tests: when
......
......@@ -13,7 +13,7 @@ Introductions to all the key parts of Django you'll need to know:
templates
class-based-views/index
files
testing
testing/index
auth
cache
conditional-view-processing
......
......@@ -135,7 +135,7 @@ table once ``syncdb`` has created it. After creating a database user with these
permissions, you'll specify the details in your project's settings file,
see :setting:`DATABASES` for details.
If you're using Django's :doc:`testing framework</topics/testing>` to test
If you're using Django's :doc:`testing framework</topics/testing/index>` to test
database queries, Django will need permission to create a test database.
.. _PostgreSQL: http://www.postgresql.org/
......
This diff is collapsed.
===================
Django and doctests
===================
Doctests use Python's standard :mod:`doctest` module, which searches your
docstrings for statements that resemble a session of the Python interactive
interpreter. A full explanation of how :mod:`doctest` works is out of the scope
of this document; read Python's official documentation for the details.
.. admonition:: What's a **docstring**?
A good explanation of docstrings (and some guidelines for using them
effectively) can be found in :pep:`257`:
A docstring is a string literal that occurs as the first statement in
a module, function, class, or method definition. Such a docstring
becomes the ``__doc__`` special attribute of that object.
For example, this function has a docstring that describes what it does::
def add_two(num):
"Return the result of adding two to the provided number."
return num + 2
Because tests often make great documentation, putting tests directly in
your docstrings is an effective way to document *and* test your code.
As with unit tests, for a given Django application, the test runner looks for
doctests in two places:
* The ``models.py`` file. You can define module-level doctests and/or a
doctest for individual models. It's common practice to put
application-level doctests in the module docstring and model-level
doctests in the model docstrings.
* A file called ``tests.py`` in the application directory -- i.e., the
directory that holds ``models.py``. This file is a hook for any and all
doctests you want to write that aren't necessarily related to models.
This example doctest is equivalent to the example given in the unittest section
above::
# models.py
from django.db import models
class Animal(models.Model):
"""
An animal that knows how to make noise
# Create some animals
>>> lion = Animal.objects.create(name="lion", sound="roar")
>>> cat = Animal.objects.create(name="cat", sound="meow")
# Make 'em speak
>>> lion.speak()
'The lion says "roar"'
>>> cat.speak()
'The cat says "meow"'
"""
name = models.CharField(max_length=20)
sound = models.CharField(max_length=20)
def speak(self):
return 'The %s says "%s"' % (self.name, self.sound)
When you :ref:`run your tests <running-tests>`, the test runner will find this
docstring, notice that portions of it look like an interactive Python session,
and execute those lines while checking that the results match.
In the case of model tests, note that the test runner takes care of creating
its own test database. That is, any test that accesses a database -- by
creating and saving model instances, for example -- will not affect your
production database. However, the database is not refreshed between doctests,
so if your doctest requires a certain state you should consider flushing the
database or loading a fixture. (See the section on :ref:`fixtures
<topics-testing-fixtures>` for more on this.) Note that to use this feature,
the database user Django is connecting as must have ``CREATE DATABASE``
rights.
For more details about :mod:`doctest`, see the Python documentation.
=================
Testing in Django
=================
.. toctree::
:hidden:
overview
doctests
advanced
Automated testing is an extremely useful bug-killing tool for the modern
Web developer. You can use a collection of tests -- a **test suite** -- to
solve, or avoid, a number of problems:
* When you're writing new code, you can use tests to validate your code
works as expected.
* When you're refactoring or modifying old code, you can use tests to
ensure your changes haven't affected your application's behavior
unexpectedly.
Testing a Web application is a complex task, because a Web application is made
of several layers of logic -- from HTTP-level request handling, to form
validation and processing, to template rendering. With Django's test-execution
framework and assorted utilities, you can simulate requests, insert test data,
inspect your application's output and generally verify your code is doing what
it should be doing.
The best part is, it's really easy.
Unit tests v. doctests
======================
There are two primary ways to write tests with Django, corresponding to the
two test frameworks that ship in the Python standard library. The two
frameworks are:
* **Unit tests** -- tests that are expressed as methods on a Python class
that subclasses :class:`unittest.TestCase` or Django's customized
:class:`TestCase`. For example::
import unittest
class MyFuncTestCase(unittest.TestCase):
def testBasic(self):
a = ['larry', 'curly', 'moe']
self.assertEqual(my_func(a, 0), 'larry')
self.assertEqual(my_func(a, 1), 'curly')
* **Doctests** -- tests that are embedded in your functions' docstrings and
are written in a way that emulates a session of the Python interactive
interpreter. For example::
def my_func(a_list, idx):
"""
>>> a = ['larry', 'curly', 'moe']
>>> my_func(a, 0)
'larry'
>>> my_func(a, 1)
'curly'
"""
return a_list[idx]
Which should I use?
-------------------
Because Django supports both of the standard Python test frameworks, it's up to
you and your tastes to decide which one to use. You can even decide to use
*both*.
For developers new to testing, however, this choice can seem confusing. Here,
then, are a few key differences to help you decide which approach is right for
you:
* If you've been using Python for a while, :mod:`doctest` will probably feel
more "pythonic". It's designed to make writing tests as easy as possible,
so it requires no overhead of writing classes or methods. You simply put
tests in docstrings. This has the added advantage of serving as
documentation (and correct documentation, at that!). However, while
doctests are good for some simple example code, they are not very good if
you want to produce either high quality, comprehensive tests or high
quality documentation. Test failures are often difficult to debug
as it can be unclear exactly why the test failed. Thus, doctests should
generally be avoided and used primarily for documentation examples only.
* The :mod:`unittest` framework will probably feel very familiar to
developers coming from Java. :mod:`unittest` is inspired by Java's JUnit,
so you'll feel at home with this method if you've used JUnit or any test
framework inspired by JUnit.
* If you need to write a bunch of tests that share similar code, then
you'll appreciate the :mod:`unittest` framework's organization around
classes and methods. This makes it easy to abstract common tasks into
common methods. The framework also supports explicit setup and/or cleanup
routines, which give you a high level of control over the environment
in which your test cases are run.
* If you're writing tests for Django itself, you should use :mod:`unittest`.
Where to go from here
=====================
As unit tests are preferred in Django, we treat them in detail in the
:doc:`overview` document.
:doc:`doctests` describes Django-specific features when using doctests.
You can also use any *other* Python test framework, Django provides an API and
tools for that kind of integration. They are described in the
:ref:`other-testing-frameworks` section of :doc:`advanced`.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment