对Python的Django框架中的项目进行单元测试的方法


Posted in Python onApril 11, 2016

 Python中的单元测试

我们先来回顾一下Python中的单元测试方法。
下面是一个 Python的单元测试简单的例子:

假如我们开发一个除法的功能,有的同学可能觉得很简单,代码是这样的:

def division_funtion(x, y):
  return x / y

但是这样写究竟对还是不对呢,有些同学可以在代码下面这样测试:

def division_funtion(x, y):
  return x / y
 
 
if __name__ == '__main__':
  print division_funtion(2, 1)
  print division_funtion(2, 4)
  print division_funtion(8, 3)

但是这样运行后得到的结果,自己每次都得算一下去核对一遍,很不方便,Python中有 unittest 模块,可以很方便地进行测试,详情可以文章最后的链接,看官网文档的详细介绍。

下面是一个简单的示例:

import unittest
 
 
def division_funtion(x, y):
  return x / y
 
 
class TestDivision(unittest.TestCase):
  def test_int(self):
    self.assertEqual(division_funtion(9, 3), 3)
 
  def test_int2(self):
    self.assertEqual(division_funtion(9, 4), 2.25)
 
  def test_float(self):
    self.assertEqual(division_funtion(4.2, 3), 1.4)
 
 
if __name__ == '__main__':
  unittest.main()

我简单地写了三个测试示例(不一定全面,只是示范,比如没有考虑除数是0的情况),运行后发现:

F.F
======================================================================
FAIL: test_float (__main__.TestDivision)
----------------------------------------------------------------------
Traceback (most recent call last):
 File "/Users/tu/YunPan/mydivision.py", line 16, in test_float
  self.assertEqual(division_funtion(4.2, 3), 1.4)
AssertionError: 1.4000000000000001 != 1.4
 
======================================================================
FAIL: test_int2 (__main__.TestDivision)
----------------------------------------------------------------------
Traceback (most recent call last):
 File "/Users/tu/YunPan/1.py", line 13, in test_int2
  self.assertEqual(division_funtion(9, 4), 2.25)
AssertionError: 2 != 2.25
 
----------------------------------------------------------------------
Ran 3 tests in 0.001s
 
FAILED (failures=2)

汗!发现了没,竟然两个都失败了,测试发现:

4.2除以3 等于 1.4000000000000001 不等于期望值 1.4

9除以4等于2,不等于期望的 2.25

下面我们就是要修复这些问题,再次运行测试,直到运行不报错为止。

譬如根据实际情况,假设我们只需要保留到小数点后6位,可以这样改:

def division_funtion(x, y):
  return round(float(x) / y, 6)

再次运行就不报错了:

...
----------------------------------------------------------------------
Ran 3 tests in 0.000s

 
OK

Django中的单元测试

尽早进行单元测试(UnitTest)是比较好的做法,极端的情况甚至强调“测试先行”。现在我们已经有了第一个model类和Form类,是时候开始写测试代码了。

Django支持python的单元测试(unit test)和文本测试(doc test),我们这里主要讨论单元测试的方式。这里不对单元测试的理论做过多的阐述,假设你已经熟悉了下列概念:test suite, test case, test/test action,  test data, assert等等。

在单元测试方面,Django继承python的unittest.TestCase实现了自己的django.test.TestCase,编写测试用 例通常从这里开始。测试代码通常位于app的tests.py文件中(也可以在models.py中编写,但是我不建议这样做)。在Django生成的 depotapp中,已经包含了这个文件,并且其中包含了一个测试用例的样例:

depot/depotapp/tests.py

from django.test import TestCase
class SimpleTest(TestCase):
def test_basic_addition(self):
"""
Tests that 1 + 1 always equals 2.
"""
self.assertEqual(1 + 1, 2)

你可以有几种方式运行单元测试:

  • python manage.py test:执行所有的测试用例
  • python manage.py test app_name, 执行该app的所有测试用例
  • python manage.py test app_name.case_name: 执行指定的测试用例

用第三种方式执行上面提供的样例,结果如下:

$ python manage.py test depotapp.SimpleTest
Creating test database for alias 'default'...
.
----------------------------------------------------------------------
Ran 1 test in 0.012s

OK
Destroying test database for alias 'default'...

你可能会主要到,输出信息中包括了创建和删除数据库的操作。为了避免测试数据造成的影响,测试过程会使用一个单独的数据库,关于如何指定测试数据库 的细节,请查阅Django文档。在我们的例子中,由于使用sqlite数据库,Django将默认采用内存数据库来进行测试。

下面就让我们来编写测试用例。在《Agile Web Development with Rails 4th》中,7.2节,最终实现的ProductTest代码如下:

class ProductTest < ActiveSupport::TestCase
test "product attributes must not be empty"do
product = Product.new
assert product.invalid?
assert product.errors[:title].any?
assert product.errors[:description].any?
assert product.errors[:price].any?
assert product.errors[:image_url].any?
end
test "product price must be positive"do
product = Product.new(:title => "My Book Title",
:description => "yyy",
:image_url => "zzz.jpg")
product.price = -1
assert product.invalid?
assert_equal "must be greater than or equal to 0.01",
product.errors[:price].join('; ')
product.price = 0
assert product.invalid?
assert_equal "must be greater than or equal to 0.01",
product.errors[:price].join('; ')
product.price = 1
assert product.valid?
end
def new_product(image_url)
Product.new(:title => "My Book Title",
:description => "yyy",
:price => 1,
:image_url => image_url)
end
test "image url"do
ok = %w{ fred.gif fred.jpg fred.png FRED.JPG FRED.Jpg
http://a.b.c/x/y/z/fred.gif }
bad = %w{ fred.doc fred.gif/more fred.gif.more }
ok.eachdo |name|
assert new_product(name).valid?, "#{name} shouldn't be invalid"
end
bad.eachdo |name|
assert new_product(name).invalid?, "#{name} shouldn't be valid"
end
end
test "product is not valid without a unique title"do
product = Product.new(:title => products(:ruby).title,
:description => "yyy",
:price => 1,
:image_url => "fred.gif")
assert !product.save
assert_equal "has already been taken", product.errors[:title].join('; ')
end
test "product is not valid without a unique title - i18n"do
product = Product.new(:title => products(:ruby).title,
:description => "yyy",
:price => 1,
:image_url => "fred.gif")
assert !product.save
assert_equal I18n.translate('activerecord.errors.messages.taken'),
product.errors[:title].join('; ')
end
end

对Product测试的内容包括:

1.title,description,price,image_url不能为空;

2. price必须大于零;

3. image_url必须以jpg,png,jpg结尾,并且对大小写不敏感;

4. titile必须唯一;

让我们在Django中进行这些测试。由于ProductForm包含了模型校验和表单校验规则,使用ProductForm可以很容易的实现上述测试:

depot/depotapp/tests.py

#/usr/bin/python
#coding: utf8
"""
This file demonstrates writing tests using the unittest module. These will pass
when you run "manage.py test".
Replace this with more appropriate tests for your application.
"""
from django.test import TestCase
from forms import ProductForm
class SimpleTest(TestCase):
def test_basic_addition(self):
"""
Tests that 1 + 1 always equals 2.
"""
self.assertEqual(1 + 1, 2)
class ProductTest(TestCase):
def setUp(self):
self.product = {
'title':'My Book Title',
'description':'yyy',
'image_url':'http://google.com/logo.png',
'price':1
}
f = ProductForm(self.product)
f.save()
self.product['title'] = 'My Another Book Title'
#### title,description,price,image_url不能为空
def test_attrs_cannot_empty(self):
f = ProductForm({})
self.assertFalse(f.is_valid())
self.assertTrue(f['title'].errors)
self.assertTrue(f['description'].errors)
self.assertTrue(f['price'].errors)
self.assertTrue(f['image_url'].errors)
####  price必须大于零
def test_price_positive(self):
f = ProductForm(self.product)
self.assertTrue(f.is_valid())
self.product['price'] = 0
f = ProductForm(self.product)
self.assertFalse(f.is_valid())
self.product['price'] = -1
f = ProductForm(self.product)
self.assertFalse(f.is_valid())
self.product['price'] = 1
####  image_url必须以jpg,png,jpg结尾,并且对大小写不敏感;
def test_imgae_url_endwiths(self):
url_base = 'http://google.com/'
oks = ('fred.gif', 'fred.jpg', 'fred.png', 'FRED.JPG', 'FRED.Jpg')
bads = ('fred.doc', 'fred.gif/more', 'fred.gif.more')
for endwith in oks:
self.product['image_url'] = url_base+endwith
f = ProductForm(self.product)
self.assertTrue(f.is_valid(),msg='error when image_url endwith '+endwith)
for endwith in bads:
self.product['image_url'] = url_base+endwith
f = ProductForm(self.product)
self.assertFalse(f.is_valid(),msg='error when image_url endwith '+endwith)
self.product['image_url'] = 'http://google.com/logo.png'
###  titile必须唯一
def test_title_unique(self):
self.product['title'] = 'My Book Title'
f = ProductForm(self.product)
self.assertFalse(f.is_valid())
self.product['title'] = 'My Another Book Title'

然后运行 python manage.py test depotapp.ProductTest。如同预想的那样,测试没有通过:

Creating test database for alias 'default'...
.F..
======================================================================
FAIL: test_imgae_url_endwiths (depot.depotapp.tests.ProductTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/holbrook/Documents/Dropbox/depot/../depot/depotapp/tests.py", line 65, in test_imgae_url_endwiths
self.assertTrue(f.is_valid(),msg='error when image_url endwith '+endwith)
AssertionError: False is not True : error when image_url endwith FRED.JPG

----------------------------------------------------------------------
Ran 4 tests in 0.055s

FAILED (failures=1)
Destroying test database for alias 'default'...

因为我们之前并没有考虑到image_url的图片扩展名可能会大写。修改ProductForm的相关部分如下:

def clean_image_url(self):
url = self.cleaned_data['image_url']
ifnot endsWith(url.lower(), '.jpg', '.png', '.gif'):
raise forms.ValidationError('图片格式必须为jpg、png或gif')
return url

然后再运行测试:

$ python manage.py test depotapp.ProductTest
Creating test database for alias 'default'...
....
----------------------------------------------------------------------
Ran 4 tests in 0.060s

OK
Destroying test database for alias 'default'...

测试通过,并且通过单元测试,我们发现并解决了一个bug。

 

Python 相关文章推荐
Python数组遍历的简单实现方法小结
Apr 27 Python
使用Python制作自动推送微信消息提醒的备忘录功能
Sep 06 Python
Django框架的中的setting.py文件说明详解
Oct 15 Python
python生成带有表格的图片实例
Feb 03 Python
python实现大文本文件分割
Jul 22 Python
基于Python解密仿射密码
Oct 21 Python
Python StringIO如何在内存中读写str
Jan 07 Python
Python3连接Mysql8.0遇到的问题及处理步骤
Feb 17 Python
python ffmpeg任意提取视频帧的方法
Feb 21 Python
Python selenium 加载并保存QQ群成员,去除其群主、管理员信息的示例代码
May 28 Python
python查询MySQL将数据写入Excel
Oct 29 Python
python自动化测试通过日志3分钟定位bug
Nov 20 Python
使用Python的Django框架结合jQuery实现AJAX购物车页面
Apr 11 #Python
Python的Django REST框架中的序列化及请求和返回
Apr 11 #Python
python中的错误处理
Apr 10 #Python
python脚本实现xls(xlsx)转成csv
Apr 10 #Python
Python使用gensim计算文档相似性
Apr 10 #Python
Python调用SQLPlus来操作和解析Oracle数据库的方法
Apr 09 #Python
python调用fortran模块
Apr 08 #Python
You might like
php 变量定义方法
2009/06/14 PHP
zend optimizer在wamp的基础上安装图文教程
2013/10/26 PHP
推荐一款MAC OS X 下php集成开发环境mamp
2014/11/08 PHP
php导入大量数据到mysql性能优化技巧
2014/12/29 PHP
ajax+php实现无刷新验证手机号的实例
2017/12/22 PHP
使用js正则控制input标签只允许输入的值
2013/07/29 Javascript
跟我学Nodejs(一)--- Node.js简介及安装开发环境
2014/05/20 NodeJs
IE6/IE7中JavaScript json提示缺少标识符、字符串或数字问题处理
2014/12/16 Javascript
JS验证IP,子网掩码,网关和MAC的方法
2015/07/02 Javascript
jquery实现图片预加载
2015/12/25 Javascript
好好了解一下Cookie(强烈推荐)
2016/06/14 Javascript
AngularJs验证重复密码的方法(两种)
2016/11/25 Javascript
使用JS在浏览器中判断当前网络连接状态的几种方法
2017/05/05 Javascript
解决JSON.stringify()自动将中文转译成unicode的问题
2018/01/05 Javascript
javascript变量提升和闭包理解
2018/03/12 Javascript
webpack打包js的方法
2018/03/12 Javascript
实例详解Vue项目使用eslint + prettier规范代码风格
2018/08/20 Javascript
Vue 应用中结合vux使用微信 jssdk的方法
2018/08/28 Javascript
使用Vue-scroller页面input框不能触发滑动的问题及解决方法
2020/08/08 Javascript
javascript实现多边形碰撞检测
2020/10/24 Javascript
[01:20]辉夜杯背景故事宣传片《辉夜传说》
2015/12/25 DOTA
[00:52]DOTA2齐天大圣预告片
2016/08/13 DOTA
python join方法使用详解
2019/07/30 Python
基于CSS3的animation属性实现微信拍一拍动画效果
2020/06/22 HTML / CSS
CSS3 文字动画效果
2020/11/12 HTML / CSS
美国内衣第一品牌:Hanes(恒适)
2016/07/29 全球购物
Clarins娇韵诗英国官网:来自法国的天然护肤品牌
2017/04/18 全球购物
比较基础的php面试题及答案-编程题
2012/10/14 面试题
请写出char *p与"零值"比较的if语句
2014/09/24 面试题
财务管理专业推荐信
2013/11/19 职场文书
2014年统计工作总结
2014/11/21 职场文书
淘宝客服专员岗位职责
2015/04/07 职场文书
2016年公司中秋节致辞
2015/11/26 职场文书
「我的青春恋爱物语果然有问题。-妄言录-」第20卷封面公开
2022/03/21 日漫
Python数据可视化之Seaborn的安装及使用
2022/04/19 Python
一篇文章带你掌握SQLite3基本用法
2022/06/14 数据库