Simplified tutorial's test names and docstrings.
This commit is contained in:
parent
49b9c89d40
commit
23825b2494
|
@ -171,12 +171,12 @@ Put the following in the ``tests.py`` file in the ``polls`` application:
|
||||||
from .models import Question
|
from .models import Question
|
||||||
|
|
||||||
|
|
||||||
class QuestionMethodTests(TestCase):
|
class QuestionModelTests(TestCase):
|
||||||
|
|
||||||
def test_was_published_recently_with_future_question(self):
|
def test_was_published_recently_with_future_question(self):
|
||||||
"""
|
"""
|
||||||
was_published_recently() should return False for questions whose
|
was_published_recently() returns False for questions whose pub_date
|
||||||
pub_date is in the future.
|
is in the future.
|
||||||
"""
|
"""
|
||||||
time = timezone.now() + datetime.timedelta(days=30)
|
time = timezone.now() + datetime.timedelta(days=30)
|
||||||
future_question = Question(pub_date=time)
|
future_question = Question(pub_date=time)
|
||||||
|
@ -200,7 +200,7 @@ and you'll see something like::
|
||||||
System check identified no issues (0 silenced).
|
System check identified no issues (0 silenced).
|
||||||
F
|
F
|
||||||
======================================================================
|
======================================================================
|
||||||
FAIL: test_was_published_recently_with_future_question (polls.tests.QuestionMethodTests)
|
FAIL: test_was_published_recently_with_future_question (polls.tests.QuestionModelTests)
|
||||||
----------------------------------------------------------------------
|
----------------------------------------------------------------------
|
||||||
Traceback (most recent call last):
|
Traceback (most recent call last):
|
||||||
File "/path/to/mysite/polls/tests.py", line 16, in test_was_published_recently_with_future_question
|
File "/path/to/mysite/polls/tests.py", line 16, in test_was_published_recently_with_future_question
|
||||||
|
@ -282,8 +282,8 @@ more comprehensively:
|
||||||
|
|
||||||
def test_was_published_recently_with_old_question(self):
|
def test_was_published_recently_with_old_question(self):
|
||||||
"""
|
"""
|
||||||
was_published_recently() should return False for questions whose
|
was_published_recently() returns False for questions whose pub_date
|
||||||
pub_date is older than 1 day.
|
is older than 1 day.
|
||||||
"""
|
"""
|
||||||
time = timezone.now() - datetime.timedelta(days=1)
|
time = timezone.now() - datetime.timedelta(days=1)
|
||||||
old_question = Question(pub_date=time)
|
old_question = Question(pub_date=time)
|
||||||
|
@ -291,8 +291,8 @@ more comprehensively:
|
||||||
|
|
||||||
def test_was_published_recently_with_recent_question(self):
|
def test_was_published_recently_with_recent_question(self):
|
||||||
"""
|
"""
|
||||||
was_published_recently() should return True for questions whose
|
was_published_recently() returns True for questions whose pub_date
|
||||||
pub_date is within the last day.
|
is within the last day.
|
||||||
"""
|
"""
|
||||||
time = timezone.now() - datetime.timedelta(hours=23, minutes=59, seconds=59)
|
time = timezone.now() - datetime.timedelta(hours=23, minutes=59, seconds=59)
|
||||||
recent_question = Question(pub_date=time)
|
recent_question = Question(pub_date=time)
|
||||||
|
@ -450,7 +450,7 @@ class:
|
||||||
|
|
||||||
def create_question(question_text, days):
|
def create_question(question_text, days):
|
||||||
"""
|
"""
|
||||||
Creates a question with the given `question_text` and published the
|
Create a question with the given `question_text` and published the
|
||||||
given number of `days` offset to now (negative for questions published
|
given number of `days` offset to now (negative for questions published
|
||||||
in the past, positive for questions that have yet to be published).
|
in the past, positive for questions that have yet to be published).
|
||||||
"""
|
"""
|
||||||
|
@ -458,19 +458,19 @@ class:
|
||||||
return Question.objects.create(question_text=question_text, pub_date=time)
|
return Question.objects.create(question_text=question_text, pub_date=time)
|
||||||
|
|
||||||
|
|
||||||
class QuestionViewTests(TestCase):
|
class QuestionIndexViewTests(TestCase):
|
||||||
def test_index_view_with_no_questions(self):
|
def test_no_questions(self):
|
||||||
"""
|
"""
|
||||||
If no questions exist, an appropriate message should be displayed.
|
If no questions exist, an appropriate message is displayed.
|
||||||
"""
|
"""
|
||||||
response = self.client.get(reverse('polls:index'))
|
response = self.client.get(reverse('polls:index'))
|
||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, 200)
|
||||||
self.assertContains(response, "No polls are available.")
|
self.assertContains(response, "No polls are available.")
|
||||||
self.assertQuerysetEqual(response.context['latest_question_list'], [])
|
self.assertQuerysetEqual(response.context['latest_question_list'], [])
|
||||||
|
|
||||||
def test_index_view_with_a_past_question(self):
|
def test_past_question(self):
|
||||||
"""
|
"""
|
||||||
Questions with a pub_date in the past should be displayed on the
|
Questions with a pub_date in the past are displayed on the
|
||||||
index page.
|
index page.
|
||||||
"""
|
"""
|
||||||
create_question(question_text="Past question.", days=-30)
|
create_question(question_text="Past question.", days=-30)
|
||||||
|
@ -480,9 +480,9 @@ class:
|
||||||
['<Question: Past question.>']
|
['<Question: Past question.>']
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_index_view_with_a_future_question(self):
|
def test_future_question(self):
|
||||||
"""
|
"""
|
||||||
Questions with a pub_date in the future should not be displayed on
|
Questions with a pub_date in the future aren't displayed on
|
||||||
the index page.
|
the index page.
|
||||||
"""
|
"""
|
||||||
create_question(question_text="Future question.", days=30)
|
create_question(question_text="Future question.", days=30)
|
||||||
|
@ -490,10 +490,10 @@ class:
|
||||||
self.assertContains(response, "No polls are available.")
|
self.assertContains(response, "No polls are available.")
|
||||||
self.assertQuerysetEqual(response.context['latest_question_list'], [])
|
self.assertQuerysetEqual(response.context['latest_question_list'], [])
|
||||||
|
|
||||||
def test_index_view_with_future_question_and_past_question(self):
|
def test_future_question_and_past_question(self):
|
||||||
"""
|
"""
|
||||||
Even if both past and future questions exist, only past questions
|
Even if both past and future questions exist, only past questions
|
||||||
should be displayed.
|
are displayed.
|
||||||
"""
|
"""
|
||||||
create_question(question_text="Past question.", days=-30)
|
create_question(question_text="Past question.", days=-30)
|
||||||
create_question(question_text="Future question.", days=30)
|
create_question(question_text="Future question.", days=30)
|
||||||
|
@ -503,7 +503,7 @@ class:
|
||||||
['<Question: Past question.>']
|
['<Question: Past question.>']
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_index_view_with_two_past_questions(self):
|
def test_two_past_questions(self):
|
||||||
"""
|
"""
|
||||||
The questions index page may display multiple questions.
|
The questions index page may display multiple questions.
|
||||||
"""
|
"""
|
||||||
|
@ -521,20 +521,19 @@ Let's look at some of these more closely.
|
||||||
First is a question shortcut function, ``create_question``, to take some
|
First is a question shortcut function, ``create_question``, to take some
|
||||||
repetition out of the process of creating questions.
|
repetition out of the process of creating questions.
|
||||||
|
|
||||||
``test_index_view_with_no_questions`` doesn't create any questions, but checks
|
``test_no_questions`` doesn't create any questions, but checks the message:
|
||||||
the message: "No polls are available." and verifies the ``latest_question_list``
|
"No polls are available." and verifies the ``latest_question_list`` is empty.
|
||||||
is empty. Note that the :class:`django.test.TestCase` class provides some
|
Note that the :class:`django.test.TestCase` class provides some additional
|
||||||
additional assertion methods. In these examples, we use
|
assertion methods. In these examples, we use
|
||||||
:meth:`~django.test.SimpleTestCase.assertContains()` and
|
:meth:`~django.test.SimpleTestCase.assertContains()` and
|
||||||
:meth:`~django.test.TransactionTestCase.assertQuerysetEqual()`.
|
:meth:`~django.test.TransactionTestCase.assertQuerysetEqual()`.
|
||||||
|
|
||||||
In ``test_index_view_with_a_past_question``, we create a question and verify that it
|
In ``test_past_question``, we create a question and verify that it appears in
|
||||||
appears in the list.
|
the list.
|
||||||
|
|
||||||
In ``test_index_view_with_a_future_question``, we create a question with a
|
In ``test_future_question``, we create a question with a ``pub_date`` in the
|
||||||
``pub_date`` in the future. The database is reset for each test method, so the
|
future. The database is reset for each test method, so the first question is no
|
||||||
first question is no longer there, and so again the index shouldn't have any
|
longer there, and so again the index shouldn't have any questions in it.
|
||||||
questions in it.
|
|
||||||
|
|
||||||
And so on. In effect, we are using the tests to tell a story of admin input
|
And so on. In effect, we are using the tests to tell a story of admin input
|
||||||
and user experience on the site, and checking that at every state and for every
|
and user experience on the site, and checking that at every state and for every
|
||||||
|
@ -565,21 +564,21 @@ in the future is not:
|
||||||
.. snippet::
|
.. snippet::
|
||||||
:filename: polls/tests.py
|
:filename: polls/tests.py
|
||||||
|
|
||||||
class QuestionIndexDetailTests(TestCase):
|
class QuestionDetailViewTests(TestCase):
|
||||||
def test_detail_view_with_a_future_question(self):
|
def test_future_question(self):
|
||||||
"""
|
"""
|
||||||
The detail view of a question with a pub_date in the future should
|
The detail view of a question with a pub_date in the future
|
||||||
return a 404 not found.
|
returns a 404 not found.
|
||||||
"""
|
"""
|
||||||
future_question = create_question(question_text='Future question.', days=5)
|
future_question = create_question(question_text='Future question.', days=5)
|
||||||
url = reverse('polls:detail', args=(future_question.id,))
|
url = reverse('polls:detail', args=(future_question.id,))
|
||||||
response = self.client.get(url)
|
response = self.client.get(url)
|
||||||
self.assertEqual(response.status_code, 404)
|
self.assertEqual(response.status_code, 404)
|
||||||
|
|
||||||
def test_detail_view_with_a_past_question(self):
|
def test_past_question(self):
|
||||||
"""
|
"""
|
||||||
The detail view of a question with a pub_date in the past should
|
The detail view of a question with a pub_date in the past
|
||||||
display the question's text.
|
displays the question's text.
|
||||||
"""
|
"""
|
||||||
past_question = create_question(question_text='Past Question.', days=-5)
|
past_question = create_question(question_text='Past Question.', days=-5)
|
||||||
url = reverse('polls:detail', args=(past_question.id,))
|
url = reverse('polls:detail', args=(past_question.id,))
|
||||||
|
|
Loading…
Reference in New Issue