码迷,mamicode.com
  •  
  • 首页
  • Web开发
  • Windows程序
  • 编程语言
  • 数据库
  • 移动开发
  • 系统相关
  • 微信
  • 其他好文
  • 会员
  •  
首页 > 编程语言 > 详细

【Python技巧系列】unittest:python自带测试模块【转载】

时间:2015-09-25 21:36:31      阅读:264      评论:0      收藏:0      [点我收藏+]

标签:

****本文转载自http://www.cnpythoner.com/post/303.html****

 

  1 python unittest单元测试方法和用例
  2 
  3 python内部自带了一个单元测试的模块,pyUnit也就是我们说的:unittest
  4 
  5 先介绍下unittest的基本使用方法:
  6 
  7 1.import unittest
  8 2.定义一个继承自unittest.TestCase的测试用例类
  9 3.定义setUp和tearDown,在每个测试用例前后做一些辅助工作。
 10 4.定义测试用例,名字以test开头。
 11 5.一个测试用例应该只测试一个方面,测试目的和测试内容应很明确。主要是调用assertEqual、assertRaises等断言方法判断程序执行结果和预期值是否相符。
 12 6.调用unittest.main()启动测试
 13 7.如果测试未通过,会输出相应的错误提示。如果测试全部通过则不显示任何东西,这时可以添加-v参数显示详细信息。
 14 
 15 下面是unittest模块的常用方法:
 16 assertEqual(a, b)     a == b      
 17 assertNotEqual(a, b)     a != b      
 18 assertTrue(x)     bool(x) is True      
 19 assertFalse(x)     bool(x) is False      
 20 assertIs(a, b)     a is b     2.7
 21 assertIsNot(a, b)     a is not b     2.7
 22 assertIsNone(x)     x is None     2.7
 23 assertIsNotNone(x)     x is not None     2.7
 24 assertIn(a, b)     a in b     2.7
 25 assertNotIn(a, b)     a not in b     2.7
 26 assertIsInstance(a, b)     isinstance(a, b)     2.7
 27 assertNotIsInstance(a, b)     not isinstance(a, b)     2.7
 28 
 29 下面看具体的代码应用:
 30 
 31 首先写了一个简单应用:
 32 import random
 33 import unittest
 34 
 35 class TestSequenceFunctions(unittest.TestCase):
 36 
 37    def setUp(self):
 38        self.seq = range(10)
 39 
 40    def test_shuffle(self):
 41        # make sure the shuffled sequence does not lose any elements
 42        random.shuffle(self.seq)
 43        self.seq.sort()
 44        self.assertEqual(self.seq, range(10))
 45 
 46        # should raise an exception for an immutable sequence
 47        self.assertRaises(TypeError, random.shuffle, (1,2,3))
 48 
 49    def test_choice(self):
 50        element = random.choice(self.seq)
 51        self.assertTrue(element in self.seq)
 52 
 53    def test_error(self):
 54           element = random.choice(self.seq)
 55           self.assertTrue(element not in self.seq)
 56 
 57 
 58 if __name__ == ‘__main__‘:
 59    unittest.main()
 60 
 61 下面是写了一个简单的应用,测试下面4个网址返回的状态码是否是200。
 62 import unittest
 63 import urllib
 64 
 65 class TestUrlHttpcode(unittest.TestCase):
 66    def setUp(self):
 67        urlinfo = [‘http://www.baidu.com‘,‘http://www.163.com‘,‘http://www.sohu.com‘,‘http://www.cnpythoner.com‘]
 68        self.checkurl = urlinfo
 69 
 70    def test_ok(self):
 71        for m in self.checkurl:
 72            httpcode = urllib.urlopen(m).getcode()
 73            self.assertEqual(httpcode,200)
 74 
 75 if __name__ == ‘__main__‘:
 76    unittest.main()
 77 
 78 如果有的网址打不开,返回404的话,测试则会报错
 79 
 80  如果有的网址打不开,返回404的话,测试则会报错
 81 
 82   ERROR: test_ok (__main__.TestUrlHttpcode)
 83 ----------------------------------------------------------------------
 84 Traceback (most recent call last):
 85   File "jay.py", line 12, in test_ok
 86     httpcode = urllib.urlopen(m).getcode()
 87   File "/usr/lib/python2.7/urllib.py", line 86, in urlopen
 88     return opener.open(url)
 89   File "/usr/lib/python2.7/urllib.py", line 207, in open
 90     return getattr(self, name)(url)
 91   File "/usr/lib/python2.7/urllib.py", line 462, in open_file
 92     return self.open_local_file(url)
 93   File "/usr/lib/python2.7/urllib.py", line 476, in open_local_file
 94     raise IOError(e.errno, e.strerror, e.filename)
 95 IOError: [Errno 2] No such file or directory: ‘fewfe.com‘
 96 
 97 ----------------------------------------------------------------------
 98 Ran 1 test in 1.425s
 99 
100 FAILED (errors=1)
101  
102 
103 也有其他的unittest方法,用于执行更具体的检查,如:
104 
105 Method     Checks that     New in
106 assertAlmostEqual(a, b)     round(a-b, 7) == 0      
107 assertNotAlmostEqual(a, b)     round(a-b, 7) != 0      
108 assertGreater(a, b)     a > b     2.7
109 assertGreaterEqual(a, b)     a >= b     2.7
110 assertLess(a, b)     a < b     2.7
111 assertLessEqual(a, b)     a <= b     2.7
112 assertRegexpMatches(s, re)     regex.search(s)     2.7
113 assertNotRegexpMatches(s, re)     not regex.search(s)     2.7
114 assertItemsEqual(a, b)     sorted(a) == sorted(b) and works with unhashable objs     2.7
115 assertDictContainsSubset(a, b)     all the key/value pairs in a exist in b     2.7
116 assertMultiLineEqual(a, b)     strings     2.7
117 assertSequenceEqual(a, b)     sequences     2.7
118 assertListEqual(a, b)     lists     2.7
119 assertTupleEqual(a, b)     tuples     2.7
120 assertSetEqual(a, b)     sets or frozensets     2.7
121 assertDictEqual(a, b)     dicts     2.7
122 assertMultiLineEqual(a, b)     strings     2.7
123 assertSequenceEqual(a, b)     sequences     2.7
124 assertListEqual(a, b)     lists     2.7
125 assertTupleEqual(a, b)     tuples     2.7
126 assertSetEqual(a, b)     sets or frozensets     2.7
127 assertDictEqual(a, b)     dicts     2.7
128 
129 你可以用unittest模块的更多方法来做自己的单元测试。

 

 

【Python技巧系列】unittest:python自带测试模块【转载】

标签:

原文地址:http://www.cnblogs.com/manqing/p/4839462.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
分享档案
更多>
2021年07月29日 (22)
2021年07月28日 (40)
2021年07月27日 (32)
2021年07月26日 (79)
2021年07月23日 (29)
2021年07月22日 (30)
2021年07月21日 (42)
2021年07月20日 (16)
2021年07月19日 (90)
2021年07月16日 (35)
周排行
mamicode.com排行更多图片
更多
  • Spring Cloud 从入门到精通(一)Nacos 服务中心初探  2021-07-29
  • 基础的排序算法  2021-07-29
  • SpringBoot|常用配置介绍  2021-07-29
  • 关于 .NET 与 JAVA 在 JIT 编译上的一些差异  2021-07-29
  • C语言常用函数-toupper()将字符转换为大写英文字母函数  2021-07-29
  • 《手把手教你》系列技巧篇(十)-java+ selenium自动化测试-元素定位大法之By class name(详细教程)  2021-07-28
  • 4-1 YAML配置文件 注入 JavaBean中  2021-07-28
  • 【python】 用来将对象持久化的 pickle 模块  2021-07-28
  • 马拉车算法  2021-07-28
  • 用Python进行冒泡排序  2021-07-28
友情链接
兰亭集智   国之画   百度统计   站长统计   阿里云   chrome插件   新版天听网
关于我们 - 联系我们 - 留言反馈
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!