python學習-unittest筆記
阿新 • • 發佈:2018-09-14
etc .text clean up lena this add 測試的 pcl log
創建被測試的文件 mathfunc.py
def add(a, b): return a+b def minus(a, b): return a-b def multi(a, b): return a*b def divide(a, b): return a/b
創建測試的文件 mathfunc_test.py
# !/usr/bin/env python # -*- coding:utf-8 -*- import unittest from mathfunc import * class TestMathFunc(unittest.TestCase): @classmethod def setUpClass(cls): print("This setUpClass() method only called once.") @classmethod def tearDownClass(cls): print("This tearDownClass() method only called once too.") def setUp(self): print("do something before test.Prepare environment.") def tearDown(self): print("do something after test.Clean up.") def test_add(self): print("add") result=add(1,2) self.assertEqual(3,result) self.assertNotEqual(1,result) def test_minus(self): print("minus") result=minus(2,1) self.assertEqual(1,result) def test_multi(self): print("multi") result=multi(2,3) self.assertEqual(6,result) @unittest.skip("don‘t run") def test_divide(self): print("devide") result=divide(8,2) self.assertEqual(4,result) self.assertEqual(3,result) if __name__ == ‘__main__‘: unittest.main(verbosity=2)
執行測試 創建 mathfunc_testsuit.py 並生成htlm 報告
#!/usr/bin/env python # -*- coding:utf-8 -*- import unittest from mathfunc_test import TestMathFunc from HTMLTestRunnerNew import HTMLTestRunner import time import os if __name__ == ‘__main__‘: suite=unittest.TestSuite() # tests=[TestMathFunc("test_add"),TestMathFunc("test_minus")] # suite.addTests(tests) suite.addTests(unittest.TestLoader().loadTestsFromTestCase(TestMathFunc)) now = time.strftime(‘%Y-%m-%d %H-%M-%S‘) print(now) filename = open(os.getcwd() + ‘/testResult_report‘ + now + ‘.html‘, ‘wb‘) runner = HTMLTestRunner( stream=filename, title=‘單元測試報告1‘, description=‘單元測試報告描述‘, tester=‘test001‘) runner.run(suite) # with open("mathlog.txt","a")as f: # runner=unittest.TextTestRunner(stream=f,verbosity=2) # runner.run(suite)
備註
生成html報告 的方法
轉自 https://www.cnblogs.com/lizhe860/p/9041693.html
python學習-unittest筆記