博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
django 学习笔记 (五)
阅读量:6222 次
发布时间:2019-06-21

本文共 7483 字,大约阅读时间需要 24 分钟。

hot3.png

五、Writing your first Django app, part 5

自动测试的好处

  • 节省开发时间
  • 避免错误
  • 让别人能够接受你的代码,否则无法确认质量
  • 帮助小组协同开发

第一个测试的例子

在前面的 polls 中有个小 bug : Poll.was_published_recently() 在Poll的发布时间在未来的时候也返回 True

通过 admin页面可以很容易的验证,通过 Shell 也能够检测出这个bug。 python manage.py shell

>>> 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

1.写一个测试来暴露出这个bug

通常项目在建立的时候会自动建立一个模拟的tests.py文件,全部删除后改成这样。

import datetimefrom django.utils import timezonefrom django.test import TestCasefrom polls.models import Pollclass 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() 来验证。

2.运行测试

python manage.py test polls

测试的执行流程

  • 在polls内寻找tests.py
  • 寻找django.test.TestCase的子类
  • 建立用于测试的数据库
  • 寻找以 test开头的方法
  • 建立实例
  • 使用 assertEqual() 检测结果

3.修复 bug

polls/models.py

def was_published_recently(self):    now = timezone.now()    return now - datetime.timedelta(days=1) <= self.pub_date <  now

再次运行测试,通过

更复杂一点的测试

  • 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的行为

1.测试工具 client

  • 建立测试环境

    >>> 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']

2.改进视图

不显示未来发表的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的方式来筛选记录

3.测试改进后的新视图

可以使用浏览器测试,也可以通过增加 tests.py的方式测试。

from django.core.urlresolvers import reversedef 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'],            ['
'] ) 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'], ['
'] ) 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'], ['
', '
'] )
  • 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

4.测试 DetailView

防止猜测来直接访问未来的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, 测试未来的始终报错

测试的原则

  • 测试的名字包含功能的描述
  • 每个测试条件一个测试方法
  • 每个 Model 或者 view 一个测试类

转载于:https://my.oschina.net/bailiangcn/blog/229125

你可能感兴趣的文章
C# WinForm 拖动无边框窗体 改变无边框窗体尺寸
查看>>
C++赋值函数详解
查看>>
删除vector中的偶数元素,删除list中的奇数元素
查看>>
循环-11. 水仙花数(20)
查看>>
奇偶校验算法
查看>>
boost:库program_options--第一篇
查看>>
分享最新的博客到LinkedIn Timeline
查看>>
MySQL主从架构之Master-Master互为主备
查看>>
discuz mysqli_connect() 不支持 advice_mysqli_connect
查看>>
KMP算法详解 --- 彻头彻尾理解KMP算法
查看>>
细嗅Promise
查看>>
Jquery中$与$.fn的差别
查看>>
MongoDB入门简单介绍
查看>>
Mining 任务分类
查看>>
HTML常见元素集锦
查看>>
[JQuery]用InsertAfter实现图片走马灯展示效果
查看>>
C实现通用数据结构--单链表
查看>>
zookeeper适用场景:分布式锁实现
查看>>
grep命令參数及使用方法
查看>>
用Lighttpd做图片服务器
查看>>