TestCase:html
一段Testcase代碼示例:python
# -*- coding: utf-8 -*- ''' Created on 2019-6-27 @author: adminstrator ''' import unittest class UnitTestDemo1(unittest.TestCase): def setUp(self): print('Run before test') def tearDown(self): print('Run after test') def testCase(self): print('test case....') if __name__ == "__main__": #import sys;sys.argv = ['', 'Test.testName'] unittest.main()
斷言:測試
# -*- coding: utf-8 -*- ''' Created on 2019-6-27 @author: adminstrator ''' import unittest class AssertTestCase(unittest.TestCase): ''' 斷言演示 ''' def setUp(self): print('Run befor testcase') def tearDown(self): print('Run after testcase') def testAssert(self): num1=12 num2=10 self.assertEqual(num1, num2, '兩個數不相等') #最後一個參數msg是當測試用例不經過時輸出的內容,當斷言不經過時,不會繼續執行下面的內容 self.assertNotEqual(num1, num2, '兩個數相等') #self.調用類的私有方法或私有變量 if __name__ == "__main__": #import sys;sys.argv = ['', 'Test.testName'] unittest.main()
TestSuite:ui
# -*- coding: utf-8 -*- ''' Created on 2019-6-28 @author: adminstrator ''' import unittest class UserCase(unittest.TestCase): def testAddUser(self): print('add a user') def testDelUser(self): print('delete a user') if __name__ == "__main__": #下面是3種將TestCase加到TestSuite中的方法 suite1=unittest.TestSuite(map(UserCase,['testAddUser','testDelUser'])) suite2=unittest.TestSuite() suite2.addTests(map(UserCase,['testAddUser','testDelUser'])) suite3=unittest.TestSuite() suite3.addTest(UserCase('testAddUser')) suite3.addTest(UserCase('testDelUser')) trunner=unittest.TextTestRunner() #經過TextTestRunner執行TestSuite trunner.run(suite1)
TextTestRunner:url
TestCase與TestSuite單獨文件的示例:spa
# -*- coding: utf-8 -*- ''' Created on 2019-6-28 @author: adminstrator ''' import unittest class UnitTestDemoShow(unittest.TestCase): def setUp(self): print('before testcase run') def tearDown(self): print('after testcase run') def testOne(self): str1='hello' str2='hello everyone' self.assertIn(str1, str2, '判斷包含') def testTwo(self): num1='10' num2='10' self.assertEqual(num1, num2, '判斷相等') if __name__ == "__main__": #import sys;sys.argv = ['', 'Test.testName'] unittest.main()
# -*- coding: utf-8 -*- ''' Created on 2019-6-28 @author: adminstrator ''' import unittest from UnitTestDemoShow import UnitTestDemoShow class TestSuiteFile(unittest.TestCase): def RunTestSuite(self): testsuite=unittest.TestSuite() testsuite.addTest(UnitTestDemoShow('testOne')) testsuite.addTest(UnitTestDemoShow('testTwo')) runner=unittest.TextTestRunner() runner.run(testsuite) if __name__ == "__main__": t=TestSuiteFile() t.RunTestSuite()
HTMLTestRunner下載地址:.net
http://tungwaiyip.info/software/HTMLTestRunner.html3d
下載後存放在\python\Lib\site-packages下code
驗證是否導入成功:htm
python3中用HTMLTestRunner.py報ImportError: No module named 'StringIO'如何解決
1.緣由是官網的是python2語法寫的,看官手動把官網的HTMLTestRunner.py改爲python3的語法:
2.修改彙總:
第94行,將import StringIO修改爲import io
第539行,將self.outputBuffer = StringIO.StringIO()修改爲self.outputBuffer = io.StringIO()
第642行,將if not rmap.has_key(cls):修改爲if not cls in rmap:
第766行,將uo = o.decode('latin-1')修改爲uo = e
第772行,將ue = e.decode('latin-1')修改爲ue = e
第631行,將print >> sys.stderr, '\nTime Elapsed: %s' % (self.stopTime-self.startTime)修改爲print(sys.stderr, '\nTime Elapsed: %s' % (self.stopTime-self.startTime))
生成測試報告(TestCase仍是使用以前的):
# -*- coding: utf-8 -*- ''' Created on 2019-6-28 @author: adminstrator ''' import unittest from UnitTestDemoShow import UnitTestDemoShow import time,os import HTMLTestRunner class HTMLReportDemo(unittest.TestCase): def RunTestSuite(self): testsuite=unittest.TestSuite() testsuite.addTest(UnitTestDemoShow('testOne')) testsuite.addTest(UnitTestDemoShow('testTwo')) #利用HTMLTestRunner生成測試報告 reportfile="./reports/"+str(int(round(time.time()*1000))) os.mkdir(reportfile) filename=reportfile+"report.html" fp=open(filename,'wb') runner=HTMLTestRunner.HTMLTestRunner(stream=fp,title=u'HTMLTestRunner測試報告',description=u'這是一個HTMLTestRunner測試報告') result=runner.run(testsuite) if __name__ == "__main__": t=HTMLReportDemo() t.RunTestSuite()