在前面的 polls 中有個小 bug : Poll.was_published_recently() 在Poll的發佈時間在將來的時候也返回 Truepython
經過 admin頁面能夠很容易的驗證,經過 Shell 也可以檢測出這個bug。 python manage.py shell
web
>>> import datetime >>> from django.utils import timezone >>> from polls.models import Poll >>> # create a Poll instance with pub_date 30 days in the future >>> future_poll = Poll(pub_date=timezone.now() + datetime.timedelta(days=30)) >>> # was it published recently? >>> future_poll.was_published_recently() True
一般項目在創建的時候會自動創建一個模擬的tests.py文件,所有刪除後改爲這樣。shell
import datetime from django.utils import timezone from django.test import TestCase from polls.models import Poll class PollMethodTests(TestCase): def test_was_published_recently_with_future_poll(self): """ was_published_recently() should return False for polls whose pub_date is in the future """ future_poll = Poll(pub_date=timezone.now() + datetime.timedelta(days=30)) self.assertEqual(future_poll.was_published_recently(), False)
創建一個 django.test.TestCase 的子類 PollMethodTests , 在其中創建了一個測試方法 test_was_published_recently_with_future_poll() ,使用 assertEqual() 來驗證。數據庫
python manage.py test polls
測試的執行流程django
polls/models.py瀏覽器
def was_published_recently(self): now = timezone.now() return now - datetime.timedelta(days=1) <= self.pub_date < now
再次運行測試,經過app
1天以內返回 True工具
超過1天返回 False測試
def test_was_published_recently_with_old_poll(self): """ was_published_recently() should return False for polls whose pub_date is older than 1 day """ old_poll = Poll(pub_date=timezone.now() - datetime.timedelta(days=30)) self.assertEqual(old_poll.was_published_recently(), False) def test_was_published_recently_with_recent_poll(self): """ was_published_recently() should return True for polls whose pub_date is within the last day """ recent_poll = Poll(pub_date=timezone.now() - datetime.timedelta(hours=1)) self.assertEqual(recent_poll.was_published_recently(), True)
經過工具來模擬web的行爲url
創建測試環境
>>> from django.test.utils import setup_test_environment >>> setup_test_environment()
導入測試類
>>> from django.test.client import Client >>> client = Client()
系列測試
訪問 / 返回404
>>> response = client.get('/') >>> response.status_code
使用reverse方法訪問視圖
>>> from django.core.urlresolvers import reverse >>> response = client.get(reverse('polls:index')) >>> response.status_code >>> response.content
訪問 /polls/
>>> from polls.models import Poll 創建一個新Poll >>> from django.utils import timezone >>> p = Poll(question="Who is your favorite Beatle?", pub_date=timezone.now()) >>> p.save() >>> response = client.get('/polls/') >>> response.content >>> response.context['latest_poll_list']
不顯示將來發表的Poll,修改 view.py 中的get_queryset() 方法.
from django.utils import timezone ... def get_queryset(self): """ Return the last five published polls (not including those set to be published in the future). """ return Poll.objects.filter( pub_date__lte=timezone.now() ).order_by('-pub_date')[:5]
__lte
的方式來篩選記錄可使用瀏覽器測試,也能夠經過增長 tests.py的方式測試。
from django.core.urlresolvers import reverse def create_poll(question, days): """ Creates a poll with the given `question` published the given number of `days` offset to now (negative for polls published in the past, positive for polls that have yet to be published). """ return Poll.objects.create(question=question, pub_date=timezone.now() + datetime.timedelta(days=days)) class PollViewTests(TestCase): def test_index_view_with_no_polls(self): """ If no polls exist, an appropriate message should be 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_poll_list'], []) def test_index_view_with_a_past_poll(self): """ Polls with a pub_date in the past should be displayed on the index page. """ create_poll(question="Past poll.", days=-30) response = self.client.get(reverse('polls:index')) self.assertQuerysetEqual( response.context['latest_poll_list'], ['<Poll: Past poll.>'] ) def test_index_view_with_a_future_poll(self): """ Polls with a pub_date in the future should not be displayed on the index page. """ create_poll(question="Future poll.", days=30) response = self.client.get(reverse('polls:index')) self.assertContains(response, "No polls are available.", status_code=200) self.assertQuerysetEqual(response.context['latest_poll_list'], []) def test_index_view_with_future_poll_and_past_poll(self): """ Even if both past and future polls exist, only past polls should be displayed. """ create_poll(question="Past poll.", days=-30) create_poll(question="Future poll.", days=30) response = self.client.get(reverse('polls:index')) self.assertQuerysetEqual( response.context['latest_poll_list'], ['<Poll: Past poll.>'] ) def test_index_view_with_two_past_polls(self): """ The polls index page may display multiple polls. """ create_poll(question="Past poll 1.", days=-30) create_poll(question="Past poll 2.", days=-5) response = self.client.get(reverse('polls:index')) self.assertQuerysetEqual( response.context['latest_poll_list'], ['<Poll: Past poll 2.>', '<Poll: Past poll 1.>'] )
create_poll 用於構造數據
空Poll狀況
test_index_view_with_no_polls
正常顯示過去的Poll
test_index_view_with_a_past_poll
將來的不顯示
test_index_view_with_a_future_poll
過去將來同時存在,只顯示過去
test_index_view_with_future_poll_and_past_poll
顯示多個polls
test_index_view_with_two_past_polls
防止猜想來直接訪問將來的poll
修改 DetailView方法
class DetailView(generic.DetailView): ... def get_queryset(self): """ Excludes any polls that aren't published yet. """ return Poll.objects.filter(pub_date__lte=timezone.now()) Poll.objects.filter(pub_date__lte=timezone.now())
增長測試
訪問將來的poll test_detail_view_with_a_future_poll
訪問過去的poll test_detail_view_with_a_past_poll
class PollIndexDetailTests(TestCase): def test_detail_view_with_a_future_poll(self): """ The detail view of a poll with a pub_date in the future should return a 404 not found. """ future_poll = create_poll(question='Future poll.', days=5) response = self.client.get(reverse('polls:detail', args=(future_poll.id,))) self.assertEqual(response.status_code, 404) def test_detail_view_with_a_past_poll(self): """ The detail view of a poll with a pub_date in the past should display the poll's question. """ past_poll = create_poll(question='Past Poll.', days=-5) response = self.client.get(reverse('polls:detail', args=(past_poll.id,))) self.assertContains(response, past_poll.question, status_code=200)
若是設置 handler404, 狀態返回始終是 200, 測試將來的始終報錯