使用PYTHON的UNITTEST做测试
来源:互联网 发布:家具摆放软件 编辑:程序博客网 时间:2024/05/21 10:48
在练习的时候遇到问题, 在test.py中 用unittest.main(),不能找到测试函数。原因:测试函数必须要以小写的test开头,unittet才能找到。
顺便找到些好的帖子,收藏http://www.cnblogs.com/imouren/archive/2011/08/04/2127997.html
文档地址http://docs.python.org/library/unittest.html
第一个例子直接引用文档中的例子,其他的简略的写了写结论性的东西,具体的参考python_testing_cookbook一书。
一、简单的例子
1 import random 2 import unittest 3 4 class TestSequenceFunctions(unittest.TestCase): 5 6 def setUp(self): 7 self.seq = range(10) 8 9 def test_shuffle(self):10 # make sure the shuffled sequence does not lose any elements11 random.shuffle(self.seq)12 self.seq.sort()13 self.assertEqual(self.seq, range(10))14 15 # should raise an exception for an immutable sequence16 self.assertRaises(TypeError, random.shuffle, (1,2,3))17 18 def test_choice(self):19 element = random.choice(self.seq)20 self.assertTrue(element in self.seq)21 22 def test_sample(self):23 with self.assertRaises(ValueError):24 random.sample(self.seq, 20)25 for element in random.sample(self.seq, 5):26 self.assertTrue(element in self.seq)27 28 if __name__ == '__main__':29 unittest.main()
实现一个继承unittest.TestCase的类,使用unittest.main()来测试该类中所有以test开头的测试用例。
简单的assert方法有如下:
self.assertEquals(100, value)
self.assertTrue(value == 0)
self.assertFalse(value > 0
self.assertRaises(TypeError, value.convert_to_decimal)
在写测试用例的时候尽量使用assertEquals而不是assertTrue、assertFalse,当assertEquals失败的时候,会打印出比较的两方值更直观
self.fail([msg])会无条件的导致测试失败,不推荐使用。
下面的例子的写法是要避免发生:
1 import unittest2 class BadTest(unittest.TestCase):3 def test_no_roman_numeral(self):4 value = RomanNumeralConverter(None)5 try:6 value.convert_to_decimal()7 self.fail("Expected a TypeError")8 except TypeError, e:9 pass
这个时候推推荐使用assertRaises
二、使用setUp和tearDown
每次运行test方法时,先用setUp初始化程序,然后运行test方法,最后使用tearDown清理程序
def setUp(self):
print "Creating a new RomanNumeralConverter..."
self.cvt = RomanNumeralConverter()
def tearDown(self):
print "Destroying the RomanNumeralConverter..."
self.cvt = None
如果不同的方法需要不同的setUp和tearDown方法,那么只好写过个测试用例了。
三、使用比unittest.main()更详细的运行测试用例的方法
if __name__ == "__main__":
suite = unittest.TestLoader().loadTestsFromTestCase( \
RomanNumeralConverterTest)
unittest.TextTestRunner(verbosity=2).run(suite)
python使用unittest.TestLoader().loadTestsFromTestCase方法获取RomanNumeralConverterTest的所有test方法
然后通过TextTestRunner来运行
四、运行部分测试方法
1 if __name__ == "__main__": 2 import sys 3 suite = unittest.TestSuite() 4 if len(sys.argv) == 1: 5 suite = unittest.TestLoader().loadTestsFromTestCase(\ 6 RomanNumeralConverterTest) 7 else: 8 for test_name in sys.argv[1:]: 9 suite.addTest(\10 RomanNumeralConverterTest(test_name))11 unittest.TextTestRunner(verbosity=2).run(suite)
五、一次运行多个测试用例
1 if __name__ == "__main__":2 import unittest3 from recipe5 import *4 suite1 = unittest.TestLoader().loadTestsFromTestCase( \5 RomanNumeralConverterTest)6 suite2 = unittest.TestLoader().loadTestsFromTestCase( \7 RomanNumeralComboTest)8 suite = unittest.TestSuite([suite1, suite2])9 unittest.TextTestRunner(verbosity=2).run(suite)
六、在test model 中 使用test suites
1 def high_and_low(): 2 suite = unittest.TestSuite() 3 suite.addTest(\ 4 RomanNumeralConverterTest("test_parsing_millenia")) 5 suite.addTest(\ 6 RomanNumeralConverterTest("test_parsing_one")) 7 return suite 8 9 def combos():10 return unittest.TestSuite(map(RomanNumeralConverterTest,\11 ["test_combo1", "test_combo2", "test_combo3"]))12 13 def all():14 return unittest.TestLoader().loadTestsFromTestCase(\15 RomanNumeralConverterTest)16 17 if __name__ == "__main__":18 for suite_func in [high_and_low, combos, all]:19 print "Running test suite '%s'" % suite_func.func_name20 suite = suite_func()21 unittest.TextTestRunner(verbosity=2).run(suite)
七、将已有的测试代码迁移到unittest
不使用unittest
1 from recipe7 import * 2 3 class RomanNumeralTester(object): 4 def __init__(self): 5 self.cvt = RomanNumeralConverter() 6 7 def simple_test(self): 8 print "+++ Converting M to 1000" 9 assert self.cvt.convert_to_decimal("M") == 100010 11 def combo_test1(self):12 print "+++ Converting MMX to 2010"13 assert self.cvt.convert_to_decimal("MMXX") == 201014 15 def combo_test2(self):16 print "+++ Converting MMMMDCLXVIII to 4668"17 val = self.cvt.convert_to_decimal("MMMMDCLXVII")18 self.check(val, 4668)19 20 def other_test(self):21 print "+++ Converting MMMM to 4000"22 val = self.cvt.convert_to_decimal("MMMM")23 self.check(val, 4000)24 25 def check(self, actual, expected):26 if (actual != expected):27 raise AssertionError("%s doesn't equal %s" % \28 (actual, expected))29 30 def test_the_system(self):31 self.simple_test()32 self.combo_test1()33 self.combo_test2()34 self.other_test()35 36 if __name__ == "__main__":37 tester = RomanNumeralTester()38 tester.test_the_system()
迁移到unittest
1 from recipe7 import * 2 from recipe7_legacy import * 3 import unittest 4 5 if __name__ == "__main__": 6 tester = RomanNumeralTester() 7 8 suite = unittest.TestSuite() 9 for test in [tester.simple_test, tester.combo_test1, \10 tester.combo_test2, tester.other_test]:11 testcase = unittest.FunctionTestCase(test)12 suite.addTest(testcase)13 14 unittest.TextTestRunner(verbosity=2).run(suite)
八、简单化
一个测试函数里面写一个assert***,便于查看bug
九、测试边缘
比如测试一个到4000为止的整数,需要测试
1,4000, 0 4001, -1, None, 1.2
错误的输入也是必要的
通过迭代测试边缘
1 def test_bad_inputs(self): 2 r = self.cvt.convert_to_roman 3 d = self.cvt.convert_to_decimal 4 edges = [("equals", r, "", None),\ 5 ("equals", r, "I", 1.2),\ 6 ("raises", d, TypeError, None),\ 7 ("raises", d, TypeError, 1.2)\ 8 ] 9 [self.checkout_edge(edge) for edge in edges]10 11 def checkout_edge(self, edge):12 if edge[0] == "equals":13 f, output, input = edge[1], edge[2], ed14 print("Converting %s to %s..." % (input15 self.assertEquals(output, f(input))16 elif edge[0] == "raises":17 f, exception, args = edge[1], edge[2], 18 print("Converting %s, expecting %s" % \19 (args, exception))20 self.assertRaises(exception, f, *args)
- 使用PYTHON的UNITTEST做测试
- Python的unittest做参数化测试
- 使用python的unittest测试类
- python 使用unittest 测试接口
- python unittest做api自动化测试
- 【python】python的unittest使用
- Python中使用unittest做UT
- python unittest的简单使用
- Python的unittest自动化测试框架
- python测试之unittest
- python测试之unittest
- python unittest 测试框架
- python 测试框架 unittest
- Python-unittest测试框架
- python unittest 自动化测试
- python unittest框架的使用&解析
- Python测试代码2之--unittest类的测试
- python 单元测试的模块 pyUnit(unittest),mock与Python测试
- 【学习点滴-数据结构-栈&队列】 用两个栈模拟一个队列
- 一步一步走进块驱动之第二章
- Mongodb VS Hbase
- ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using passwor
- 商业银行清算业务(二)——系统内人民币清算
- 使用PYTHON的UNITTEST做测试
- informix dbschema 常用语句
- minicom能输出串口信息不能输入
- Objective-C超越C++,成为第三大受欢迎编程语言
- Java 序列化的高级认识
- hibernate 使用的集合类(list和bag)
- extjs ajax 通用写法
- 在linux shell中使用ftp命令来实现自动登陆、上传与下载
- 将一张表的字段根据条件更新到另一张表的一个字段中去