python unittest基本介绍
python内部自带了一个单元测试的模块,pyUnit也就是我们说的:unittest
1、介绍下unittest的基本使用方法:
1)import unittest
2)定义一个继承自unittest.TestCase的测试用例类
3)定义setUp和tearDown,在每个测试用例前后做一些辅助工作。
4)定义测试用例,名字以test开头。
5)一个测试用例应该只测试一个方面,测试目的和测试内容应很明确。主要是调用assertEqual、assertRaises等断言方法判断程序执行结果和预期值是否相符。
6)调用unittest.main()启动测试
7)如果测试未通过,会输出相应的错误提示。如果测试全部通过则不显示任何东西,这时可以添加-v参数显示详细信息。
2、 下面是unittest模块的常用方法:
assertEqual(a, b) a == b
assertNotEqual(a, b) a != b
assertTrue(x) bool(x) is True
assertFalse(x) bool(x) is False
assertIs(a, b) a is b 2.7
assertIsNot(a, b) a is not b 2.7
assertIsNone(x) x is None 2.7
assertIsNotNone(x) x is not None 2.7
assertIn(a, b) a in b 2.7
assertNotIn(a, b) a not in b 2.7
assertIsInstance(a, b) isinstance(a, b) 2.7
assertNotIsInstance(a, b) not isinstance(a, b) 2.7
下面看具体的代码应用:
首先写了一个简单应用:
import random
import unittest
class TestSequenceFunctions(unittest.TestCase):
def setUp(self):
self.seq = range(10)
def test_shuffle(self):
# make sure the shuffled sequence does not lose any elements
random.shuffle(self.seq)
self.seq.sort()
self.assertEqual(self.seq, range(10))
# should raise an exception for an immutable sequence
self.assertRaises(TypeError, random.shuffle, (1,2,3))
element = random.choice(self.seq)
self.assertTrue(element inself.seq)
def test_error(self):
element = random.choice(self.seq)
self.assertTrue(element not in self.seq)
if __name__ == '__main__':
unittest.main()
下面是写了一个简单的应用,测试下面4个网址返回的状态码是否是200。
import unittest
import urllib
class TestUrlHttpcode(unittest.TestCase):
def setUp(self):
urlinfo = ['http://www.baidu.com','http://www.163.com','http://www.sohu.com','http://www.cnpythoner.com']
self.checkurl = urlinfo
def test_ok(self):
for m in
self.checkurl:
httpcode = urllib.urlopen(m).getcode()
self.assertEqual(httpcode,200)
if __name__ == '__main__':
unittest.main()
如果有的网址打不开,返回404的话,测试则会报错
如果有的网址打不开,返回404的话,测试则会报错
ERROR: test_ok (__main__.TestUrlHttpcode)
----------------------------------------------------------------------
Traceback (most recent call last):
File "jay.py", line 12, in test_ok
httpcode = urllib.urlopen(m).getcode()
File "/usr/lib/python2.7/urllib.py", line 86, in urlopen
return opener.open(url)
File "/usr/lib/python2.7/urllib.py", line 207, in open
return getattr(self, name)(url)
File "/usr/lib/python2.7/urllib.py", line 462, in open_file
return self.open_local_file(url)
File "/usr/lib/python2.7/urllib.py", line 476, in
open_local_file
raise IOError(e.errno, e.strerror, e.filename)
IOError: [Errno 2] No such file or directory: 'fewfe.com'
----------------------------------------------------------------------
Ran 1 test in 1.425s
FAILED (errors=1)
3也有其他的unittest方法,用于执行更具体的检查,如:
Method Checks that New in
assertAlmostEqual(a, b) round(a-b, 7) == 0
assertNotAlmostEqual(a, b) round(a-b, 7) != 0
assertGreater(a, b) a > b 2.7
assertGreaterEqual(a, b) a >= b 2.7
assertLess(a, b) a < b 2.7
assertLessEqual(a, b) a <= b 2.7
assertRegexpMatches(s, re) regex.search(s)
2.7
assertNotRegexpMatches(s, re) not regex.search(s)
2.7
assertItemsEqual(a, b) sorted(a) == sorted(b) and works with
unhashable objs 2.7
assertDictContainsSubset(a, b) all the key/value pairs in a
exist in b 2.7
assertMultiLineEqual(a, b) strings 2.7
assertSequenceEqual(a, b) sequences 2.7
assertListEqual(a, b) lists 2.7
assertTupleEqual(a, b) tuples 2.7
assertSetEqual(a, b) sets or frozensets
2.7
assertDictEqual(a, b) dicts 2.7
assertMultiLineEqual(a, b) strings 2.7
assertSequenceEqual(a, b) sequences 2.7
assertListEqual(a, b) lists 2.7
assertTupleEqual(a, b) tuples 2.7
assertSetEqual(a, b) sets or frozensets
2.7
assertDictEqual(a, b) dicts 2.7
你可以用unittest模块的更多方法来做自己的单元测试。
python unittest基本介绍的更多相关文章
- python+unittest 搭建简易的接口测试框架
主要介绍如何使用python+unittest快速搭建一个接口测试的框架 1.安装python unittest 2.新建一个python项目ApiTest 在setUp和setDown里设置一些需 ...
- python strip()函数 介绍
python strip()函数 介绍,需要的朋友可以参考一下 函数原型 声明:s为字符串,rm为要删除的字符序列 s.strip(rm) 删除s字符串中开头.结尾处,位于 rm删除 ...
- 从python run 和python unittest两种eclipse运行方式深入理解if __name__ == "__main__"
在写一个简单的python测试程序的时候,发现eclipse中Run as "Python run 和 Python unittest”结果不一样?为什么会不一样? 先贴一下代码段: # - ...
- Python 科学计算-介绍
Python 科学计算 作者 J.R. Johansson (robert@riken.jp) http://dml.riken.jp/~rob/ 最新版本的 IPython notebook 课程文 ...
- Python 基于python操纵zookeeper介绍
基于python操纵zookeeper介绍 by:授客 QQ:1033553122 测试环境 Win7 64位 Python 3.3.4 kazoo-2.6.1-py2.py3-none-any.w ...
- Python redis 简单介绍
Python redis 简单介绍 1.安装 终端输入: pip(or)pip3.6 install redis 安装成功 2.哈哈,发现我并没有redis服务可以访问,所以到这里,在本机安装了red ...
- 自动化测试神器 之 python unittest 断言
自动化测试的最后一步需要判断结果是否正确,而正确设置断言可以帮助判断测试用例的执行结果,从而提高自动化测试的效率,python unittest 提供了一个比较完整的断言方法.unittest框架测 ...
- 第二种方式,修改python unittest的执行顺序,使用猴子补丁
1.按照测试用例的上下顺序,而不是按方法的名称的字母顺序来执行测试用例. 之前的文章链接 python修改python unittest的运行顺序 之前写的,不是猴子补丁,而是要把Test用例的类名传 ...
- python 函数参数介绍
python 函数参数介绍 python 使用过程总,总会遇到 *args,**kw形式的参数,总是一头雾水,而且网上介绍的或是叫法不一,为此专门深入实践进而了解了函数参数的使用 具体请看代码 #-* ...
随机推荐
- Swift :?和 !
Swift语言使用var定义变量,但和别的语言不同,Swift里不会自动给变量赋初始值, 也就是说变量不会有默认值,所以要求使用变量之前必须要对其初始化 .如果在使用变量之前不进行初始化就会报错: v ...
- file_get_contents函数和curl函数不能用
某天file_get_contents()突然不能用了,检查了下php配置文件 allow_url_fopen=on 没问题 各种重启也没用 最后在ssh下执行 chmod 755 /etc/re ...
- [转]ubuntu server上网配置
[转]ubuntu server上网配置 http://blog.sina.com.cn/s/blog_6c9d65a101011pyt.html 今天我的ubuntu server上不去网了,所以重 ...
- 5 给我们的c#程序添加注释
注释是你的程序中的一个重要部分.在程序中添加注释是用来告诉你和其他人你的程序是做什么用的,你的思路是怎样的.注释可以用你熟悉的中文进行添加. 当你想暂时把你程序中的某些语句去掉的时候,不需要把他们删除 ...
- 委托、匿名委托和lambda表达式
1.委托 在.NET中,委托有点类似于C/C++中的函数指针,但与指针不同的是,委托是一种安全的类型,那么我们就以实现两个数的差为例,先声明一个成员方法: public int CompareTwoV ...
- 3、颜色的字符串、十进制、十六进制相互转换(color convert between dec、hex and string )
int color_int=***; 1.(十进制整数)转换成(十六进制的字符串) String color_hex = String.format("#%06X", (0xFFF ...
- 【Construct Binary Tree from Preorder and Inorder Traversal】cpp
题目: Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume t ...
- [转载]char * 和char []的区别---之第一篇
char * 和char []的区别---之第一篇 原文地址http://blog.csdn.net/yahohi/article/details/7427724 在C/C++中,指针和数组在很多地 ...
- CocoaPods最佳实践探讨
近期在项目中首次使用了CocoaPods.从软件工程的角度来看,我对目前常见的CocoaPods使用方法有些意见,建议做一些改进.先说一下我建议的最佳实践,后面再分析为什么要这样做.并且希望大家根据自 ...
- hibernate学习笔记--可选的配置属性
3.4. 可选的配置属性 有大量属性能用来控制Hibernate在运行期的行为. 它们都是可选的, 并拥有适当的默认值. 警告: 其中一些属性是"系统级(system-level)的&qu ...