mirror of
https://github.com/django/django.git
synced 2024-12-22 09:05:43 +00:00
Fixed #11593 -- Added testing to reusable apps intro
This commit is contained in:
parent
bcbc4b9b8a
commit
8c78be660f
@ -314,6 +314,72 @@ working. We'll now fix this by installing our new ``django-polls`` package.
|
||||
|
||||
#. Run the development server to confirm the project continues to work.
|
||||
|
||||
Testing your app
|
||||
================
|
||||
|
||||
Since we moved the ``polls`` directory out of the project, we don't have the
|
||||
project's ``settings.py`` and ``urls.py`` anymore. We need another way to
|
||||
configure our app in order to test it.
|
||||
|
||||
#. First create a ``tests`` directory and move the tests over from the
|
||||
``polls`` directory. Because the tests now reside in a different module the
|
||||
relative import ``from .models import Poll`` has to be changed to
|
||||
``from polls.models import Poll``.
|
||||
|
||||
#. Now create a ``settings.py`` inside the ``tests`` folder which will contain
|
||||
only the settings required to test the application.
|
||||
|
||||
.. code-block:: python
|
||||
:caption: ``tests/settings.py``
|
||||
|
||||
from django.urls import include, path
|
||||
|
||||
SECRET_KEY = "fake-key"
|
||||
|
||||
INSTALLED_APPS = [
|
||||
"polls",
|
||||
# adding tests allows us to define models inside the tests package
|
||||
# "tests",
|
||||
]
|
||||
|
||||
DATABASES = {
|
||||
"default": {
|
||||
"ENGINE": "django.db.backends.sqlite3",
|
||||
"NAME": ":memory:",
|
||||
}
|
||||
}
|
||||
|
||||
TEMPLATES = [
|
||||
{
|
||||
"BACKEND": "django.template.backends.django.DjangoTemplates",
|
||||
"APP_DIRS": True,
|
||||
},
|
||||
]
|
||||
|
||||
ROOT_URLCONF = "tests.urls"
|
||||
|
||||
#. Because the test involves views, a :setting:`ROOT_URLCONF` that includes our
|
||||
app is needed. Create an appropriate ``urls.py`` inside the ``tests``
|
||||
directory.
|
||||
|
||||
.. code-block:: python
|
||||
:caption: ``tests/urls.py``
|
||||
|
||||
from django.urls import include, path
|
||||
|
||||
urlpatterns = [
|
||||
path("polls/", include("polls.urls")),
|
||||
]
|
||||
|
||||
#. Now run the tests.
|
||||
|
||||
.. code-block:: shell
|
||||
|
||||
python -m django test --settings=tests.settings tests
|
||||
|
||||
For more information on testing reusable applications see :ref:`Advanced
|
||||
testing topics <testing-reusable-applications>`.
|
||||
|
||||
Publishing your app
|
||||
===================
|
||||
|
||||
|
@ -421,7 +421,6 @@ following structure:
|
||||
|
||||
.. code-block:: text
|
||||
|
||||
runtests.py
|
||||
polls/
|
||||
__init__.py
|
||||
models.py
|
||||
@ -429,11 +428,70 @@ following structure:
|
||||
tests/
|
||||
__init__.py
|
||||
models.py
|
||||
test_settings.py
|
||||
settings.py
|
||||
tests.py
|
||||
urls.py
|
||||
|
||||
Let's take a look inside a couple of those files:
|
||||
|
||||
.. code-block:: python
|
||||
:caption: ``tests/settings.py``
|
||||
|
||||
from django.urls import include, path
|
||||
|
||||
SECRET_KEY = "fake-key"
|
||||
|
||||
INSTALLED_APPS = [
|
||||
"polls",
|
||||
# adding tests allows us to define models inside the tests package
|
||||
"tests",
|
||||
]
|
||||
|
||||
DATABASES = {
|
||||
"default": {
|
||||
"ENGINE": "django.db.backends.sqlite3",
|
||||
"NAME": ":memory:",
|
||||
}
|
||||
}
|
||||
|
||||
TEMPLATES = [
|
||||
{
|
||||
"BACKEND": "django.template.backends.django.DjangoTemplates",
|
||||
"APP_DIRS": True,
|
||||
},
|
||||
]
|
||||
|
||||
ROOT_URLCONF = "tests.urls"
|
||||
|
||||
This file contains the :doc:`Django settings </topics/settings>` required to
|
||||
run your app's tests.
|
||||
|
||||
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.
|
||||
|
||||
|
||||
.. code-block:: python
|
||||
:caption: ``tests/urls.py``
|
||||
|
||||
from django.urls import include, path
|
||||
|
||||
urlpatterns = [
|
||||
path("polls/", include("polls.urls")),
|
||||
]
|
||||
|
||||
If you want to test views, you need a :setting:`ROOT_URLCONF` that includes
|
||||
your app.
|
||||
|
||||
|
||||
You can then invoke the test runner using
|
||||
|
||||
.. code-block:: shell
|
||||
|
||||
python -m django test --settings=tests.settings tests
|
||||
|
||||
If you need a more complicated setup, you can create a custom ``runtests.py``
|
||||
to manually initialize the test runner.
|
||||
|
||||
.. code-block:: python
|
||||
:caption: ``runtests.py``
|
||||
|
||||
@ -446,7 +504,7 @@ Let's take a look inside a couple of those files:
|
||||
from django.test.utils import get_runner
|
||||
|
||||
if __name__ == "__main__":
|
||||
os.environ["DJANGO_SETTINGS_MODULE"] = "tests.test_settings"
|
||||
os.environ["DJANGO_SETTINGS_MODULE"] = "tests.settings"
|
||||
django.setup()
|
||||
TestRunner = get_runner(settings)
|
||||
test_runner = TestRunner()
|
||||
@ -454,33 +512,14 @@ Let's take a look inside a couple of those files:
|
||||
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.
|
||||
You can use use this script 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.
|
||||
|
||||
.. code-block:: python
|
||||
:caption: ``tests/test_settings.py``
|
||||
|
||||
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.
|
||||
|
||||
|
||||
.. _other-testing-frameworks:
|
||||
|
||||
Using different testing frameworks
|
||||
|
Loading…
Reference in New Issue
Block a user