Simplified tutorial's test names and docstrings.

This commit is contained in:
Tim Graham 2017-06-07 09:47:15 -04:00 committed by GitHub
parent 49b9c89d40
commit 23825b2494
1 changed files with 35 additions and 36 deletions

View File

@ -171,12 +171,12 @@ Put the following in the ``tests.py`` file in the ``polls`` application:
from .models import Question
class QuestionMethodTests(TestCase):
class QuestionModelTests(TestCase):
def test_was_published_recently_with_future_question(self):
"""
was_published_recently() should return False for questions whose
pub_date is in the future.
was_published_recently() returns False for questions whose pub_date
is in the future.
"""
time = timezone.now() + datetime.timedelta(days=30)
future_question = Question(pub_date=time)
@ -200,7 +200,7 @@ and you'll see something like::
System check identified no issues (0 silenced).
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):
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):
"""
was_published_recently() should return False for questions whose
pub_date is older than 1 day.
was_published_recently() returns False for questions whose pub_date
is older than 1 day.
"""
time = timezone.now() - datetime.timedelta(days=1)
old_question = Question(pub_date=time)
@ -291,8 +291,8 @@ more comprehensively:
def test_was_published_recently_with_recent_question(self):
"""
was_published_recently() should return True for questions whose
pub_date is within the last day.
was_published_recently() returns True for questions whose pub_date
is within the last day.
"""
time = timezone.now() - datetime.timedelta(hours=23, minutes=59, seconds=59)
recent_question = Question(pub_date=time)
@ -450,7 +450,7 @@ class:
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
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)
class QuestionViewTests(TestCase):
def test_index_view_with_no_questions(self):
class QuestionIndexViewTests(TestCase):
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'))
self.assertEqual(response.status_code, 200)
self.assertContains(response, "No polls are available.")
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.
"""
create_question(question_text="Past question.", days=-30)
@ -480,9 +480,9 @@ class:
['<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.
"""
create_question(question_text="Future question.", days=30)
@ -490,10 +490,10 @@ class:
self.assertContains(response, "No polls are available.")
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
should be displayed.
are displayed.
"""
create_question(question_text="Past question.", days=-30)
create_question(question_text="Future question.", days=30)
@ -503,7 +503,7 @@ class:
['<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.
"""
@ -521,20 +521,19 @@ Let's look at some of these more closely.
First is a question shortcut function, ``create_question``, to take some
repetition out of the process of creating questions.
``test_index_view_with_no_questions`` doesn't create any questions, but checks
the message: "No polls are available." and verifies the ``latest_question_list``
is empty. Note that the :class:`django.test.TestCase` class provides some
additional assertion methods. In these examples, we use
``test_no_questions`` doesn't create any questions, but checks the message:
"No polls are available." and verifies the ``latest_question_list`` is empty.
Note that the :class:`django.test.TestCase` class provides some additional
assertion methods. In these examples, we use
:meth:`~django.test.SimpleTestCase.assertContains()` and
:meth:`~django.test.TransactionTestCase.assertQuerysetEqual()`.
In ``test_index_view_with_a_past_question``, we create a question and verify that it
appears in the list.
In ``test_past_question``, we create a question and verify that it appears in
the list.
In ``test_index_view_with_a_future_question``, we create a question with a
``pub_date`` in the future. The database is reset for each test method, so the
first question is no longer there, and so again the index shouldn't have any
questions in it.
In ``test_future_question``, we create a question with a ``pub_date`` in the
future. The database is reset for each test method, so the first question is no
longer there, and so again the index shouldn't have any questions in it.
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
@ -565,21 +564,21 @@ in the future is not:
.. snippet::
:filename: polls/tests.py
class QuestionIndexDetailTests(TestCase):
def test_detail_view_with_a_future_question(self):
class QuestionDetailViewTests(TestCase):
def test_future_question(self):
"""
The detail view of a question with a pub_date in the future should
return a 404 not found.
The detail view of a question with a pub_date in the future
returns a 404 not found.
"""
future_question = create_question(question_text='Future question.', days=5)
url = reverse('polls:detail', args=(future_question.id,))
response = self.client.get(url)
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
display the question's text.
The detail view of a question with a pub_date in the past
displays the question's text.
"""
past_question = create_question(question_text='Past Question.', days=-5)
url = reverse('polls:detail', args=(past_question.id,))