2017.09.03 python第15次笔记
来源:互联网 发布:荥经网络花店 编辑:程序博客网 时间:2024/06/05 14:29
0. something
最近去了一趟湖南,前后9天,再加上之前的准备,已经有两周没有好好学习了!要加快学习进度了
- 继续看Tutorials,今天看《Part 5: Testing》;
- 对网站有一个初步的设计,作出一个基本模型,然后分步设计实施;
- 买戒指,尽快!打报告,尽快!
下面开始今天的工作:
1. What are automated tests?
- 特定的模型方法是否能返回期望的值?细节
- 用户的一系列输入是否产生想要的结果?整体
自动化测试就是你一次创建一个测试集,当你每次对你的app做出修改以后,你都可以使用这个测试集来检查你的代码是否还像你原来希望的那样工作,而不用再去执行耗时的人工测试!
2. Why you need to create tests
So why create tests, and why now?
Tests will save you time
Tests don’t just identify problems, they prevent them
Tests make your code more attractive
Tests help teams work together
3. Basic testing strategies
测试驱动开发:再写代码之前就写好测试!(测试先于代码)
4. Writing our first test
We identify a bug
先用admin创建一个question,这个question的时间为距离今天不止一天的未来。然后再shell里对这个question调用was_published_recently()
,结果如下
看到结果为True,这不是符合逻辑的结果,也就是一个小bug
Create a test to expose the bug
from django.test import TestCase# Create your tests here.import datetimefrom django.utils import timezonefrom .models import Questionclass QuestionModelTests(TestCase): def test_was_published_recently_with_future_question(self): """ :was_published_recently returns False for questions whose pub_date is in the future.: """ time = timezone.now() + datetime.timedelta(days=30) future_question = Question(pub_date=time) self.assertIs(future_question.was_published_recently(), False)
Running tests
E:\python3\Django\mysite>python manage.py test pollsCreating test database for alias 'default'...System check identified no issues (0 silenced).F======================================================================FAIL: test_was_published_recently_with_future_question (polls.tests.QuestionModelTests)----------------------------------------------------------------------Traceback (most recent call last): File "E:\python3\Django\mysite\polls\tests.py", line 18, 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.002sFAILED (failures=1)Destroying test database for alias 'default'...
Fixing the bug
def was_published_recently(self): # return self.pub_date >= timezone.now() - datetime.timedelta(days=1) now = timezone.now() return now - datetime.timedelta(days=1) < self.pub_date < now
重新运行测试:
E:\python3\Django\mysite>python manage.py test pollsCreating test database for alias 'default'...System check identified no issues (0 silenced)..----------------------------------------------------------------------Ran 1 test in 0.000sOKDestroying test database for alias 'default'...
More comprehensive tests
def test_was_published_recently_with_old_question(self): """ was_published_recently() returns False for questions whose pub_date is older than 1 day. """ time = timezone.now() - datetime.timedelta(days=1, seconds=1) old_question = Question(pub_date=time) self.assertIs(old_question.was_published_recently(), False)def test_was_published_recently_with_recent_question(self): """ was_published_recently() returns True for questions whose pub_date is within the last day. """ time = timezone.now() - datetime.timedelta(hours=23, minutes=59, seconds=59) recent_question = Question(pub_date=time) self.assertIs(recent_question.was_published_recently(), True)
运行结果:
E:\python3\Django\mysite>python manage.py test pollsCreating test database for alias 'default'...System check identified no issues (0 silenced)....----------------------------------------------------------------------Ran 3 tests in 0.003sOKDestroying test database for alias 'default'...
阅读全文
0 0
- 2017.09.03 python第15次笔记
- 2017.09.10 python第16次笔记
- 2017.09.10 python第17次笔记
- 2017.09.16 python第18次笔记
- 2017.08.18 python第14次笔记
- 2017.06.23 python套接字第1次笔记
- Python自学-第11次作业
- Python自学-第12次作业
- Python自学-第16次作业
- J2EE 第5次课笔记--Servlet
- J2EE 第2次课笔记--Servlet
- J2EE 第2次课笔记--Servlet
- 第1次笔记:github入门学习
- python 字符串第n次出现的位置
- JSP学习笔记(第2次):Servlet过滤器
- 高级算法日记2:第1次课笔记
- 高级算法日记4:第2次课笔记
- python使用笔记(第一篇)
- CSS3的弹性盒子
- 广播--BroadcastReceiver2
- java回调函数的理解
- 如何线程安全地遍历List:Vector、CopyOnWriteArrayList
- Qt学习之旅---4.图片查看器
- 2017.09.03 python第15次笔记
- 京东笔试题目
- CodeForces 850A(105/600)
- 数据降维(基于PCA)
- 算法 第四版 1.4.12
- 程序员面试系列——有符号数的溢出
- 整数划分
- php常用函数总结表
- GLSL渲染语言入门与VBO、VAO使用:绘制一个三角形