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. ubuntu14.04:php7+apache2+mysql

    apache2: sudo apt-get install apache2 apache2-dev service apache2 start mysql: sudo apt-get install ...

  2. [uiautomator篇] 基类

      package com.softwinner.performance.benchmark; /** * UiAssistant public class * @author liuzhipeng ...

  3. Codeforces Round #204 (Div. 2)

    D. Jeff and Furik time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  4. NYOJ——239月老的难题(二分图最大匹配)

    月老的难题 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 月老准备给n个女孩与n个男孩牵红线,成就一对对美好的姻缘. 现在,由于一些原因,部分男孩与女孩可能结成幸福的一家, ...

  5. BZOJ 1443 [JSOI2009]游戏Game ——博弈论

    好题. 首先看到棋盘,先黑白染色. 然后就是二分图的经典模型. 考虑最特殊的情况,完美匹配,那么先手必胜, 因为无论如何,先手走匹配边,后手无论走哪条边,总有对应的匹配边. 如果在不在最大匹配中出发, ...

  6. cf299C Weird Game

    Weird Game Yaroslav, Andrey and Roman can play cubes for hours and hours. But the game is for three, ...

  7. Ubuntu MySQL的安装使用

    删除 mysql sudo apt-get autoremove --purge mysql-server-5.0 sudo apt-get remove mysql-server sudo apt- ...

  8. Java学习:一 开篇

    呃 工作中要用到Android开发,呃 不巧的是,关于Java关于Android,当初也只是浅浅的了解了一下.....真是书到用时方恨少了.. 趁现在工作不是太忙,还是花点时间来学习一下吧. 写写博客 ...

  9. 洛谷—— P1051 谁拿了最多奖学金

    https://www.luogu.org/problem/show?pid=1051 题目描述 某校的惯例是在每学期的期末考试之后发放奖学金.发放的奖学金共有五种,获取的条件各自不同: 1) 院士奖 ...

  10. Java修饰符关键字的顺序

    Java语言规范建议按以下顺序列出修饰符: 1. Annotations 2. public 3. protected 4. private 5. abstract 6. static 7. fina ...