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库的更多相关文章

  1. python第三方库系列之十九--python測试使用的mock库

    一.为什么须要mock         在写unittest的时候,假设系统中有非常多外部依赖,我们不须要也不希望把全部的部件都执行一遍.比方,要验证分享到微博的功能,假设每次測试的时候都要真实地把接 ...

  2. Python之路【第十八篇】:Web框架们

    Python之路[第十八篇]:Web框架们   Python的WEB框架 Bottle Bottle是一个快速.简洁.轻量级的基于WSIG的微型Web框架,此框架只由一个 .py 文件,除了Pytho ...

  3. 十八. Python基础(18)常用模块

    十八. Python基础(18)常用模块 1 ● 常用模块及其用途 collections模块: 一些扩展的数据类型→Counter, deque, defaultdict, namedtuple, ...

  4. Web 前端开发人员和设计师必读文章推荐【系列二十八】

    <Web 前端开发精华文章推荐>2014年第7期(总第28期)和大家见面了.梦想天空博客关注 前端开发 技术,分享各类能够提升网站用户体验的优秀 jQuery 插件,展示前沿的 HTML5 ...

  5. OSGi 系列(十八)之 基于注解的 Blueprint

    OSGi 系列(十八)之 基于注解的 Blueprint 1. 注解实现 blueprint 第一步:bundle 添加 Bundle-Blueprint-Annotation <plugin& ...

  6. Java 设计模式系列(十八)备忘录模式(Memento)

    Java 设计模式系列(十八)备忘录模式(Memento) 备忘录模式又叫做快照模式(Snapshot Pattern)或Token模式,是对象的行为模式.备忘录对象是一个用来存储另外一个对象内部状态 ...

  7. 踩坑系列《十》Python pip 安装问题一站式解决

    在使用Python编程语言时,难免要安装第三方库 安装一般都是在cmd命令行窗口安装 1.常规安装 ,在窗口输入 pip install 你要下载的库 这种方式一般网速比较慢,毕竟是从国外下载的 2. ...

  8. Python之路【第十八篇】Django小项目webQQ实现

    WEBQQ的实现的几种方式 1.HTTP协议特点 首先这里要知道HTTP协议的特点:短链接.无状态! 在不考虑本地缓存的情况举例来说:咱们在连接博客园的时候,当tcp连接后,我会把我自己的http头发 ...

  9. Python之路【第十八篇】Django小项目简单BBS论坛部分内容知识点

    开发一个简单的BBS论坛 项目需求: 整体参考“抽屉新热榜” + “虎嗅网” 实现不同论坛版块 帖子列表展示 帖子评论数.点赞数展示 在线用户展示 允许登录用户发贴.评论.点赞 允许上传文件 帖子可被 ...

随机推荐

  1. win10关闭防火墙命令

    NetSh Advfirewall set allprofiles state off #关闭防火墙 Netsh Advfirewall show allprofiles #查看防火墙状态

  2. 【java基础 6】java的发展史简介

    结合到近期在做springboot框架开发遇到的关于jdk版本的问题,本篇博客,主要介绍一下java的发展历史,侧重纯文介绍每个版本的特性.--主要从理论上做个宏观的了解,不做具体的技术研究讨论! 一 ...

  3. .NET重构(二):ArrayList,List,IList的联系和区别

    导读:在机房重构的时候,为了降低耦合,不能返回DataTable型数据,而需要转换为泛型集合.我一直使用的就是IList,那天师傅过来帮我挑错,问我:你为什么一直写IList呢,不应该是List吗?好 ...

  4. soa服务治理

    SOA服务治理 文章:SOA 治理简介 文章:中小型互联网公司微服务实践-经验和教训

  5. docker管理工具推荐(linux和windows)

    1.windows. 下载dokcer toolbox即可 2.linux 推荐rancher.安装链接参考:http://www.kaimingwan.com/post/rong-qi-yu-ron ...

  6. 强大的stringstream

    [本文来自]http://www.builder.com.cn/2003/0304/83250.shtmlhttp://www.cppblog.com/alantop/archive/2007/07/ ...

  7. StoryBoard中,TableView位置总是在顶部出现空白的解决

      重设TableView的 contentInset 属性可解决. 
_tableView.contentInset = UIEdgeInsetsMake( -30, 0, 0, 0);


  8. Laravel 视图中的url

    <a href="{{ url('url') }}">url</a> <a href="{{ action('StudentControll ...

  9. Codeforces 833B The Bakery(主席树 + 决策单调性优化DP)

    题目链接 The Bakery 题目大意:目标是把$n$个数分成$k$组,每个组的值为这个组内不同的数的个数,求$k$个组的值的和的最大值. 题目分析: 这道题我的解法可能和大众解法不太一样……我用主 ...

  10. 读取编码器信息Python2.7和Python3.3版本差异及解决,一次订阅多次调用callback的解决

    1. Python3.3以字节类型返回编码器信息,b'...',BUF: b'\xc3KOO\x00OO\x00OO\x00OO\x00OO\x00\x03\x00\x00\x00\x00\x99R\ ...