Python —— unnitest 单元测试框架
一、1.unnitest概念:当有很多测试案例的时候,需要集中测试,可以设计一个unnitest单元测试框架集中测试,每个案例就是一个模块;可以生成各种形式测试日志(HTML、TXT)
2.完成功能:本程序分两个模块,test_4.py完成芯片的初始化;test_fasttest.py完成芯片的算法测试(AES、DES、RSA、MD5、CRC、SHA)。最后所有模块在all_test.py里运行。
3.本程序文件框架:
|-test_case/
|-test_4.py #模块1
|-test_fasttest.py #模块2
|-all_test.py #在这里添加运行
|-result.html #生成的log,html形式
|-log.txt #生成的log,txt形式
4.效果
二、代码
由于代码太多。。。只显示关键部分
test_4.py
1 import unittest, time, re 2 import HTMLTestRunner 3 #...more imoprt 4 #...send等接口的封装 5 6 7 class Test4(unittest.TestCase): 8 def test_4(self): 9 u"""初始化""" 10 send(apduchange('F0100000100123456789abcdeffedcba9876543210')) 11 send(apduchange('F014000000')) 12 send(apduchange('F011000000')) 13 send(apduchange('F01302c80908A000000151000000')) 14 send(apduchange('F01302c2024428')) 15 send(apduchange('F013028601C9')) 16 send(apduchange('F015000000')) 17 send(apduchange('00A4040000')) 18 send(apduchange('00A4040012')) 19 20 pass 21 22 if __name__ == "__main__": 23 testunit = unittest.TestSuite() 24 testunit.addTest(Test4('test_4')) 25 runner = unittest.TextTestRunner() 26 #filename = 'C:\\result.html' 27 #fp = file(filename,'wb') 28 #runner = HTMLTestRunner.HTMLTestRunner(stream = fp, title = 'Report_title',description = 'Report_decription') 29 runner.run(testunit) 30 print 'finish test_4(initialization)'
test_fasttest.py:
1 #..实现接口 2 class Testfasttest(unittest.TestCase): 3 def test_fasttest(self): 4 #...测试案例(各种算法) 5 if __name__ == "__main__": 6 testunit = unittest.TestSuite() 7 testunit.addTest(Testfasttest('test_fasttest')) 8 runner = unittest.TextTestRunner() 9 #filename = 'C:\\result.html' 10 #fp = file(filename,'wb') 11 #runner = HTMLTestRunner.HTMLTestRunner(stream = fp, title = 'Report_title',description = 'Report_decription') 12 runner.run(testunit) 13 print'finish test_fasttest'
all_test.py
#coding=utf-8 "Combine tests for gnosis.xml.objectify package (req 2.3+)" import unittest, doctest #import test_2, test_3,test_4 #这里需要导入测试文件 import test_4,test_fasttest import HTMLTestRunner suit = doctest.DocTestSuite() suit.addTest(unittest.makeSuite(test_4.Test4)) suit.addTest(unittest.makeSuite(test_fasttest.Testfasttest)) filename = 'C:\\result.html' fp = file(filename, 'wb') runner = HTMLTestRunner.HTMLTestRunner(stream=fp,title=u'测试部_XXX',description='Rongcard_Test') runner.run(suit)