标签:
****本文转载自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