From 550eed5dad2bf2393433b075c28e71873010a4ba Mon Sep 17 00:00:00 2001 From: JasonHomeWorkstationUbuntu Date: Tue, 20 Oct 2020 21:00:31 +1100 Subject: [PATCH] 5.1 First test --- first_django_app.md | 72 ++++++++++++++++++++++++++++++++++++++++++++- src/polls/tests.py | 18 ++++++++++-- 2 files changed, 87 insertions(+), 3 deletions(-) diff --git a/first_django_app.md b/first_django_app.md index 06cc9c1..0617d9a 100644 --- a/first_django_app.md +++ b/first_django_app.md @@ -416,4 +416,74 @@ class ResultsView(generic.DetailView): * 而这里我们使用 `template_name` 来指定模板 * 我们提供 context_object_name 属性,表示我们想使用 latest_question_list -重启服务器后,就是新的投票应用(同样的html),但是内部用了generic view \ No newline at end of file +重启服务器后,就是新的投票应用(同样的html),但是内部用了generic view + +## Part 5 Automated Test + +TDD with python. Always stick to it. + +### 5.1 First test + +#### Create/Verify a bug + +幸运的是,我们的 polls 应用现在就有一个小 bug 需要被修复:我们的要求是如果 Question 是在一天之内发布的, Question.was_published_recently() 方法将会返回 True ,然而现在这个方法在 Question 的 pub_date 字段比当前时间还晚时也会返回 True(这是个 Bug)。 + +确认bug: + +``` +>>> import datetime +>>> from django.utils import timezone +>>> from polls.models import Question +>>> # create a Question instance with pub_date 30 days in the future +>>> future_question = Question(pub_date=timezone.now() + datetime.timedelta(days=30)) +>>> # was it published recently? +>>> future_question.was_published_recently() +True +``` + +#### Create a test to verify this bug + +All tests (polls/tests.py) are in app directory + +```python +import datetime + +from django.test import TestCase +from django.utils import timezone + +from .models import Question + +class QuestionModelTests(TestCase): + + def test_was_published_recently_with_future_question(self): + """ + was_published_recently() returns False for questions whose pub_date is in future + """ + + time = timezone.now() + datetime.timedelta(days=30) + future_question = Question(pub_date=time) + self.assertIs(future_question.was_published_recently(), False) +``` + +Result + +``` +python manage.py test polls +Creating test database for alias 'default'... +System check identified no issues (0 silenced). +F +====================================================================== +FAIL: test_was_published_recently_with_future_question (polls.tests.QuestionModelTests) +was_published_recently() returns False for questions whose pub_date is in future +---------------------------------------------------------------------- +Traceback (most recent call last): + File "/home/jason/HomeWorkstation/SynologyGiteaSpace/djangoproject/src/polls/tests.py", line 17, in test_was_published_recently_with_future_question + self.assertIs(future_question.was_published_recently(), False) +AssertionError: True is not False + +---------------------------------------------------------------------- +Ran 1 test in 0.001s + +FAILED (failures=1) +Destroying test database for alias 'default'... +``` \ No newline at end of file diff --git a/src/polls/tests.py b/src/polls/tests.py index 7ce503c..63ae988 100644 --- a/src/polls/tests.py +++ b/src/polls/tests.py @@ -1,3 +1,17 @@ -from django.test import TestCase +import datetime -# Create your tests here. +from django.test import TestCase +from django.utils import timezone + +from .models import Question + +class QuestionModelTests(TestCase): + + def test_was_published_recently_with_future_question(self): + """ + was_published_recently() returns False for questions whose pub_date is in future + """ + + time = timezone.now() + datetime.timedelta(days=30) + future_question = Question(pub_date=time) + self.assertIs(future_question.was_published_recently(), False) \ No newline at end of file