Django之第一个app<19>
2015-08-31 08:57
232 查看
本节内容概括:
介绍”自动化测试”
基本的测试策略
编写第一个测试
测试一个视图
介绍”自动化测试”
自动化测试是系统为你所做的。你只需创建一系列测试一次,以后你的应用改动的时候,不必再花时间来手动测试,你仍然可以检查你的代码是否可以像你当初期待的那样工作。
基本的测试策略
一些编程人员遵循
编写第一个测试
放应用测试的地方一般是应用的
将下面的代码放到polls应用中的
这里我们创建了一个
在终端,运行我们的测试:
结果为:
测试一个视图
Django提供了一个测试
我们再次开启
接下来,我们需要导入测试客户端类:
准备好后,我们可以叫客户端为我们做一些事情了:
介绍”自动化测试”
基本的测试策略
编写第一个测试
测试一个视图
介绍”自动化测试”
自动化测试是系统为你所做的。你只需创建一系列测试一次,以后你的应用改动的时候,不必再花时间来手动测试,你仍然可以检查你的代码是否可以像你当初期待的那样工作。
基本的测试策略
一些编程人员遵循
"测试驱动式开发",他们在写代码之前就写测试了,这可能与你想的不一样,但是实际上这样对一种人非常熟悉:他们描述一个问题,然后创建一些代码来解决它。
编写第一个测试
放应用测试的地方一般是应用的
test.py文件。测试系统将会自动发现文件名以
test开头的文件中的测试。
将下面的代码放到polls应用中的
tests.py文件中:
from django.test import TestCase import datetime from django.utils import timezone from .model import Question # Create your tests here. class QuestionMethodTests(TestCase): def test_was_published_recently_with_future_question(self): time=timezone.now()+datetime.timedelta(days=30) future_question=Question(pub_date=time) self.assertEqual(future_question.was_published_recently(),False)
这里我们创建了一个
django.test.TestCase的子类,里面有一个方法,创建了一个拥有未来
pub_date的
Question实例。然后我们检查
was_published_recently()的输出时,应当是
False。
在终端,运行我们的测试:
$ python manage.py test polls
结果为:
charies:mysite weichuang$ python manage.py test polls Creating test database for alias 'default'... F ====================================================================== FAIL: test_was_published_recently_with_future_question (polls.tests.QuestionMethodTests) ---------------------------------------------------------------------- Traceback (most recent call last): File "/Users/weichuang/Desktop/mysite/polls/tests.py", line 11, in test_was_published_recently_with_future_question self.assertEqual(future_question.was_published_recently(),False) AssertionError: True != False ---------------------------------------------------------------------- Ran 1 test in 0.001s
测试一个视图
Django提供了一个测试
Client来模拟一个用户在以视图的级别与代码交互,我们可以在
test.py或者
shell中使用它。
我们再次开启
shell,这里我们需要做一些事情,而在
test.py是不用的。首先在
shell中设置测试环境:
>>> from django.test.utils import setup_test_enviroment >>> setup_test_environment()
接下来,我们需要导入测试客户端类:
>>>from django.test import Client >>>client=Client()
准备好后,我们可以叫客户端为我们做一些事情了:
>>> # get a response from '/' >>> response = client.get('/') >>> # we should expect a 404 from that address >>> response.status_code 404 >>> # on the other hand we should expect to find something at '/polls/' >>> # we'll use 'reverse()' rather than a hardcoded URL >>> from django.core.urlresolvers import reverse >>> response = client.get(reverse('polls:index')) >>> response.status_code 200 >>> response.content '\n\n\n <p>No polls are available.</p>\n\n' >>> # note - you might get unexpected results if your ``TIME_ZONE`` >>> # in ``settings.py`` is not correct. If you need to change it, >>> # you will also need to restart your shell session >>> from polls.models import Question >>> from django.utils import timezone >>> # create a Question and save it >>> q = Question(question_text="Who is your favorite Beatle?", pub_date=timezone.now()) >>> q.save() >>> # check the response once again >>> response = client.get('/polls/') >>> response.content '\n\n\n <ul>\n \n <li><a href="/polls/1/">Who is your favorite Beatle?</a></li>\n \n </ul>\n\n' >>> # If the following doesn't work, you probably omitted the call to >>> # setup_test_environment() described above >>> response.context['latest_question_list'] [<Question: Who is your favorite Beatle?>]
相关文章推荐
- (ios开发)基本数据类型和结构体的封装与解封
- Android M 新的运行时权限开发者需要知道的一切
- 微信开发大坑汇总之微信支付篇
- IIS配置文件的XML格式不正确 applicationHost.config崩溃 恢复解决办法
- 【wechat】微信开发——自定义菜单
- Swift - 集合类型
- Android编程规范
- android:windowSoftInputMode属性详解
- iOS开发系列一开发环境与Xcode介绍
- Android自学笔记之RadioGroup和RadioButton的使用
- Android自学笔记之ChenkBox复选框
- Android自学笔记之 多个TextView跑马灯的实现
- android 布局背景模糊化处理
- Java EE 6: How to implement “Stay Logged In” when user login in to the web application
- How to properly logout of a Java EE 6 Web Application after logging in
- unity3d之物体克隆
- iOS 网易彩票-2框架搭建-代码重构
- Android插件实例——360 DroidPlugin详解
- [Android] createTrack_l
- 一个优秀的Android应用从建项目开始