Skip to content

Commit 56e4a01

Browse files
committed
[1.11.x] Simplified tutorial's test names and docstrings.
Backport of 23825b2 from master
1 parent bc9c6fe commit 56e4a01

File tree

1 file changed

+35
-36
lines changed

1 file changed

+35
-36
lines changed

docs/intro/tutorial05.txt

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -171,12 +171,12 @@ Put the following in the ``tests.py`` file in the ``polls`` application:
171171
from .models import Question
172172

173173

174-
class QuestionMethodTests(TestCase):
174+
class QuestionModelTests(TestCase):
175175

176176
def test_was_published_recently_with_future_question(self):
177177
"""
178-
was_published_recently() should return False for questions whose
179-
pub_date is in the future.
178+
was_published_recently() returns False for questions whose pub_date
179+
is in the future.
180180
"""
181181
time = timezone.now() + datetime.timedelta(days=30)
182182
future_question = Question(pub_date=time)
@@ -200,7 +200,7 @@ and you'll see something like::
200200
System check identified no issues (0 silenced).
201201
F
202202
======================================================================
203-
FAIL: test_was_published_recently_with_future_question (polls.tests.QuestionMethodTests)
203+
FAIL: test_was_published_recently_with_future_question (polls.tests.QuestionModelTests)
204204
----------------------------------------------------------------------
205205
Traceback (most recent call last):
206206
File "/path/to/mysite/polls/tests.py", line 16, in test_was_published_recently_with_future_question
@@ -282,17 +282,17 @@ more comprehensively:
282282

283283
def test_was_published_recently_with_old_question(self):
284284
"""
285-
was_published_recently() should return False for questions whose
286-
pub_date is older than 1 day.
285+
was_published_recently() returns False for questions whose pub_date
286+
is older than 1 day.
287287
"""
288288
time = timezone.now() - datetime.timedelta(days=1)
289289
old_question = Question(pub_date=time)
290290
self.assertIs(old_question.was_published_recently(), False)
291291

292292
def test_was_published_recently_with_recent_question(self):
293293
"""
294-
was_published_recently() should return True for questions whose
295-
pub_date is within the last day.
294+
was_published_recently() returns True for questions whose pub_date
295+
is within the last day.
296296
"""
297297
time = timezone.now() - datetime.timedelta(hours=23, minutes=59, seconds=59)
298298
recent_question = Question(pub_date=time)
@@ -450,27 +450,27 @@ class:
450450

451451
def create_question(question_text, days):
452452
"""
453-
Creates a question with the given `question_text` and published the
453+
Create a question with the given `question_text` and published the
454454
given number of `days` offset to now (negative for questions published
455455
in the past, positive for questions that have yet to be published).
456456
"""
457457
time = timezone.now() + datetime.timedelta(days=days)
458458
return Question.objects.create(question_text=question_text, pub_date=time)
459459

460460

461-
class QuestionViewTests(TestCase):
462-
def test_index_view_with_no_questions(self):
461+
class QuestionIndexViewTests(TestCase):
462+
def test_no_questions(self):
463463
"""
464-
If no questions exist, an appropriate message should be displayed.
464+
If no questions exist, an appropriate message is displayed.
465465
"""
466466
response = self.client.get(reverse('polls:index'))
467467
self.assertEqual(response.status_code, 200)
468468
self.assertContains(response, "No polls are available.")
469469
self.assertQuerysetEqual(response.context['latest_question_list'], [])
470470

471-
def test_index_view_with_a_past_question(self):
471+
def test_past_question(self):
472472
"""
473-
Questions with a pub_date in the past should be displayed on the
473+
Questions with a pub_date in the past are displayed on the
474474
index page.
475475
"""
476476
create_question(question_text="Past question.", days=-30)
@@ -480,20 +480,20 @@ class:
480480
['<Question: Past question.>']
481481
)
482482

483-
def test_index_view_with_a_future_question(self):
483+
def test_future_question(self):
484484
"""
485-
Questions with a pub_date in the future should not be displayed on
485+
Questions with a pub_date in the future aren't displayed on
486486
the index page.
487487
"""
488488
create_question(question_text="Future question.", days=30)
489489
response = self.client.get(reverse('polls:index'))
490490
self.assertContains(response, "No polls are available.")
491491
self.assertQuerysetEqual(response.context['latest_question_list'], [])
492492

493-
def test_index_view_with_future_question_and_past_question(self):
493+
def test_future_question_and_past_question(self):
494494
"""
495495
Even if both past and future questions exist, only past questions
496-
should be displayed.
496+
are displayed.
497497
"""
498498
create_question(question_text="Past question.", days=-30)
499499
create_question(question_text="Future question.", days=30)
@@ -503,7 +503,7 @@ class:
503503
['<Question: Past question.>']
504504
)
505505

506-
def test_index_view_with_two_past_questions(self):
506+
def test_two_past_questions(self):
507507
"""
508508
The questions index page may display multiple questions.
509509
"""
@@ -521,20 +521,19 @@ Let's look at some of these more closely.
521521
First is a question shortcut function, ``create_question``, to take some
522522
repetition out of the process of creating questions.
523523

524-
``test_index_view_with_no_questions`` doesn't create any questions, but checks
525-
the message: "No polls are available." and verifies the ``latest_question_list``
526-
is empty. Note that the :class:`django.test.TestCase` class provides some
527-
additional assertion methods. In these examples, we use
524+
``test_no_questions`` doesn't create any questions, but checks the message:
525+
"No polls are available." and verifies the ``latest_question_list`` is empty.
526+
Note that the :class:`django.test.TestCase` class provides some additional
527+
assertion methods. In these examples, we use
528528
:meth:`~django.test.SimpleTestCase.assertContains()` and
529529
:meth:`~django.test.TransactionTestCase.assertQuerysetEqual()`.
530530

531-
In ``test_index_view_with_a_past_question``, we create a question and verify that it
532-
appears in the list.
531+
In ``test_past_question``, we create a question and verify that it appears in
532+
the list.
533533

534-
In ``test_index_view_with_a_future_question``, we create a question with a
535-
``pub_date`` in the future. The database is reset for each test method, so the
536-
first question is no longer there, and so again the index shouldn't have any
537-
questions in it.
534+
In ``test_future_question``, we create a question with a ``pub_date`` in the
535+
future. The database is reset for each test method, so the first question is no
536+
longer there, and so again the index shouldn't have any questions in it.
538537

539538
And so on. In effect, we are using the tests to tell a story of admin input
540539
and user experience on the site, and checking that at every state and for every
@@ -565,21 +564,21 @@ in the future is not:
565564
.. snippet::
566565
:filename: polls/tests.py
567566

568-
class QuestionIndexDetailTests(TestCase):
569-
def test_detail_view_with_a_future_question(self):
567+
class QuestionDetailViewTests(TestCase):
568+
def test_future_question(self):
570569
"""
571-
The detail view of a question with a pub_date in the future should
572-
return a 404 not found.
570+
The detail view of a question with a pub_date in the future
571+
returns a 404 not found.
573572
"""
574573
future_question = create_question(question_text='Future question.', days=5)
575574
url = reverse('polls:detail', args=(future_question.id,))
576575
response = self.client.get(url)
577576
self.assertEqual(response.status_code, 404)
578577

579-
def test_detail_view_with_a_past_question(self):
578+
def test_past_question(self):
580579
"""
581-
The detail view of a question with a pub_date in the past should
582-
display the question's text.
580+
The detail view of a question with a pub_date in the past
581+
displays the question's text.
583582
"""
584583
past_question = create_question(question_text='Past Question.', days=-5)
585584
url = reverse('polls:detail', args=(past_question.id,))

0 commit comments

Comments
 (0)