python第三方库系列之十八--python/django test库
django是属于python语音的web框架,要说django測试。也能够先说说python的測试。django能够用python的方式測试,当然,django也基于python封装了一个自己的測试库。
一、python的測试--unitest库
def my_func(a_list, idx):
return a_list[idx] import unittest
class MyFuncTestCase(unittest.TestCase):
def testBasic(self):
a = ['larry', 'curly', 'moe']
self.assertEqual(my_func(a, 0), 'larry')
self.assertEqual(my_func(a, 1), 'curly')
二、django的測试--django.utils.unittest库、django.test.client库。
(1)django.utils.unittest库
更高级一点的,引用django封装的unittest。对了,这样写另一个优点:測试case能够直接用django里的models,操作数据库很方便。
from django.utils import unittest
from myapp.models import Animal class AnimalTestCase(unittest.TestCase):
def setUp(self):
self.lion = Animal.objects.create(name="lion", sound="roar")
self.cat = Animal.objects.create(name="cat", sound="meow") def test_animals_can_speak(self):
"""Animals that can speak are correctly identified"""
self.assertEqual(self.lion.speak(), 'The lion says "roar"')
self.assertEqual(self.cat.speak(), 'The cat says "meow"')
(2)django.test.client库
django作为web的服务端,要測试其服务功能,必须模拟一个client,訪问服务端验证
from django.utils import unittest
from django.test.client import Client class SimpleTest(unittest.TestCase):
def setUp(self):
# Every test needs a client.
self.client = Client() def test_details(self):
# Issue a GET request.
response = self.client.get('/customer/details/') # Check that the response is 200 OK.
self.assertEqual(response.status_code, 200) # Check that the rendered context contains 5 customers.
self.assertEqual(len(response.context['customers']), 5)
或者更高级的。直接用from django.test.TestCase
from django.test import TestCase class SimpleTest(TestCase):
def test_details(self):
response = self.client.get('/customer/details/')
self.assertEqual(response.status_code, 200) def test_index(self):
response = self.client.get('/customer/index/')
self.assertEqual(response.status_code, 200)
三、数据库使用
假设你測试时候还须要初始化数据库,那TestCase.fixtures帮你了。
并且,django在測试的时候。会帮你创建一个新的数据库名为:test_原有数据库名。
这样能够防止真实数据库被弄脏。
你能够这样初始化你的数据库:
from django.test import TestCase
from myapp.models import Animal class AnimalTestCase(TestCase):
fixtures = ['mammals.json', 'birds'] def setUp(self):
# Test definitions as before.
call_setup_methods() def testFluffyAnimals(self):
# A test that uses the fixtures.
call_some_test_code()
那有人就问了:mammals.json怎么来的呢?格式是什么呢?事实上这个文件是用python manage.py dumpdata命令来的。有关这个命令的文档请google之。文件格式大概例如以下:
[
{
"pk": 1,
"model": "apps.animal",
"fields": {
"status": 1,
"gmt_modified": "2015-07-06T14:05:38",
"gmt_created": "2015-07-06T14:05:38",
"alive": 1,
"info": ""
}
},
{
"pk": 2,
"model": "apps.animal",
"fields": {
"status": 0,
"gmt_modified": "2015-07-06T14:05:53",
"gmt_created": "2015-07-06T14:05:53",
"alive": 1,
"info": ""
}
}
]
事实上,django.test这个包还有非常多其它的功能,不能一一列举,具体能够看django官方文档。
python第三方库系列之十八--python/django test库的更多相关文章
- python第三方库系列之十九--python測试使用的mock库
一.为什么须要mock 在写unittest的时候,假设系统中有非常多外部依赖,我们不须要也不希望把全部的部件都执行一遍.比方,要验证分享到微博的功能,假设每次測试的时候都要真实地把接 ...
- Python之路【第十八篇】:Web框架们
Python之路[第十八篇]:Web框架们 Python的WEB框架 Bottle Bottle是一个快速.简洁.轻量级的基于WSIG的微型Web框架,此框架只由一个 .py 文件,除了Pytho ...
- 十八. Python基础(18)常用模块
十八. Python基础(18)常用模块 1 ● 常用模块及其用途 collections模块: 一些扩展的数据类型→Counter, deque, defaultdict, namedtuple, ...
- Web 前端开发人员和设计师必读文章推荐【系列二十八】
<Web 前端开发精华文章推荐>2014年第7期(总第28期)和大家见面了.梦想天空博客关注 前端开发 技术,分享各类能够提升网站用户体验的优秀 jQuery 插件,展示前沿的 HTML5 ...
- OSGi 系列(十八)之 基于注解的 Blueprint
OSGi 系列(十八)之 基于注解的 Blueprint 1. 注解实现 blueprint 第一步:bundle 添加 Bundle-Blueprint-Annotation <plugin& ...
- Java 设计模式系列(十八)备忘录模式(Memento)
Java 设计模式系列(十八)备忘录模式(Memento) 备忘录模式又叫做快照模式(Snapshot Pattern)或Token模式,是对象的行为模式.备忘录对象是一个用来存储另外一个对象内部状态 ...
- 踩坑系列《十》Python pip 安装问题一站式解决
在使用Python编程语言时,难免要安装第三方库 安装一般都是在cmd命令行窗口安装 1.常规安装 ,在窗口输入 pip install 你要下载的库 这种方式一般网速比较慢,毕竟是从国外下载的 2. ...
- Python之路【第十八篇】Django小项目webQQ实现
WEBQQ的实现的几种方式 1.HTTP协议特点 首先这里要知道HTTP协议的特点:短链接.无状态! 在不考虑本地缓存的情况举例来说:咱们在连接博客园的时候,当tcp连接后,我会把我自己的http头发 ...
- Python之路【第十八篇】Django小项目简单BBS论坛部分内容知识点
开发一个简单的BBS论坛 项目需求: 整体参考“抽屉新热榜” + “虎嗅网” 实现不同论坛版块 帖子列表展示 帖子评论数.点赞数展示 在线用户展示 允许登录用户发贴.评论.点赞 允许上传文件 帖子可被 ...
随机推荐
- hdu 4764 巴什博弈
Stone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Subm ...
- 04-offsetLeft和style.left的区别
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- JSF框架整理
JSP体系结构: JSF主要优势之一就是它既是Java web 应用程序的用户界面标准又是严格遵循 模型-视图-控制器(MVC)设计模式的框架. 用户界面代码(视图)和应用程序数据和逻辑(模型)的清晰 ...
- 【Android】SharedPreference存储数据
SharedPreference存储数据 使用SharedPreference保存数据 putString(key,value) 使用SharedPreference读取数据 getString( ...
- 【bzoj3689】异或之 可持久化Trie树+堆
题目描述 给定n个非负整数A[1], A[2], ……, A[n].对于每对(i, j)满足1 <= i < j <= n,得到一个新的数A[i] xor A[j],这样共有n*(n ...
- 【Luogu】P1516青蛙的约会(线性同余方程,扩展欧几里得)
题目链接 定理:对于方程\(ax+by=c\),等价于\(a*x=c(mod b)\),有整数解的充分必要条件是c是gcd(a,b)的整数倍. ——信息学奥赛之数学一本通 避免侵权.哈哈. 两只青蛙跳 ...
- SPOJ GSS6 Can you answer these queries VI ——Splay
[题目分析] 增加了插入和删除. 直接用Splay维护就好辣! 写了一个晚上,(码力不精),最后发现更新写挂了 [代码] #include <cstdio> #include <cs ...
- 记一次Jenkins 打包异常 ERROR: Exception when publishing, exception message [Failure]
今天早上打包一直都没有问题,突然有一次打包突然出现异常现象,如下: ERROR: Exception when publishing, exception message [Failure] Buil ...
- win10安装virtualbox发生严重错误
转载自:http://blog.csdn.net/ljw124213/article/details/50545101 Windows 10 系统在安装VirtualBox即将完毕时,突然回退,提示错 ...
- Codeforces 961 E Tufurama
Discription One day Polycarp decided to rewatch his absolute favourite episode of well-known TV seri ...