''' 题目要求 1:自己写一个工具类,完成数学的加减乘除以及平方积操作 2:对每个方法写2个用例 3:针对测试用例选用不同的方法去执行,然后生成测试报告 ''' ''' 实现: 3个文件:
work_20181127_mathtool.py : 定义计算方法 work_20181127_testcase.py : 定义测试用例
work_20181127_testrun.py : 定义测试集,生成测试报告
''' 代码:
work_20181127_mathtool.py:
# -*- coding:utf-8 -*- ''' @project: jiaxy @author: Jimmy @file: work_20181127_mathtool.py @ide: PyCharm Community Edition @time: 2018-11-27 14:17 @blog: https://www.cnblogs.com/gotesting/ ''' class MathTool: # 加法 def add(a,b): res = a + b print('{0} + {1} = {2}'.format(a,b,res)) return res # 减法 def pop(a,b): res = a - b print('{0} - {1} = {2}'.format(a,b,res)) return res # 乘法 def mul(a,b): res = a * b print('{0} * {1} = {2}'.format(a,b,res)) return res # 除法 def div(a,b): res = a / b print('{0} / {1} = {2}'.format(a,b,res)) return res # 平方积 def square(a,b): res = (pow(a,2)) * (pow(b,2)) print('{0} 与 {1} 的平方积 = {2}'.format(a,b,res)) return res
work_20181127_testcase.py:
# -*- coding:utf-8 -*- ''' @project: jiaxy @author: Jimmy @file: work_20181127_testcase.py @ide: PyCharm Community Edition @time: 2018-11-27 14:30 @blog: https://www.cnblogs.com/gotesting/ ''' import unittest from work_20181127_mathtool import MathTool class TestMT(unittest.TestCase): def setUp(self): print('开始对MathTool进行运算测试!') def tearDown(self): print('测试完成!') def test_add_001(self): res = MathTool.add(10,5) expect = 15 try: self.assertEquals(expect,res) print('测试通过!') except Exception as e: raise e def test_add_002(self): res = MathTool.add(3,4) expect = 6 try: self.assertEquals(expect,res) except Exception as e: raise e def test_pop_001(self): res = MathTool.pop(6,2) expect = 4 try: self.assertEquals(expect,res) except Exception as e: raise e def test_pop_002(self): res = MathTool.pop(10,4) expect = 5 try: self.assertEquals(expect,res) except Exception as e: raise e def test_mul_001(self): res = MathTool.mul(2,2) expect = 4 try: self.assertEquals(expect,res) except Exception as e: raise e def test_mul_002(self): res = MathTool.mul(3,4) expect = 11 try: self.assertEquals(expect,res) except Exception as e: raise e def test_div_001(self): res = MathTool.div(20,5) expect = 4 try: self.assertEquals(expect,res) except Exception as e: raise e def test_div_002(self): res = MathTool.div(10,5) expect = 3 try: self.assertEquals(expect,res) except Exception as e: raise e def test_square_001(self): res = MathTool.square(2,3) expect = 36 try: self.assertEquals(expect,res) except Exception as e: raise e def test_square_002(self): res = MathTool.square(2,4) expect = 63 try: self.assertEquals(expect,res) except Exception as e: raise e
work_20181127_testrun.py:
# -*- coding:utf-8 -*- ''' @project: jiaxy @author: Jimmy @file: work_20181127_testrun.py @ide: PyCharm Community Edition @time: 2018-11-27 14:49 @blog: https://www.cnblogs.com/gotesting/ ''' import unittest import work_20181127_testcase from work_20181127_testcase import TestMT import HTMLTestRunner def run_test_01(): suite = unittest.TestSuite() loader = unittest.TestLoader() suite.addTest(loader.loadTestsFromModule(work_20181127_testcase)) with open('test_result.html','wb+') as file: runner = HTMLTestRunner.HTMLTestRunner( stream = file, verbosity = 2 ) runner.run(suite) def run_test_02(): suite = unittest.TestSuite() loader = unittest.TestLoader() suite.addTest(loader.loadTestsFromTestCase(TestMT)) with open('test_result.html','wb+') as file: runner = HTMLTestRunner.HTMLTestRunner( stream = file, verbosity = 2 ) runner.run(suite) def run_test_03(): suite = unittest.TestSuite() suite.addTest(TestMT('test_add_001')) suite.addTest(TestMT('test_add_002')) suite.addTest(TestMT('test_pop_001')) suite.addTest(TestMT('test_pop_002')) suite.addTest(TestMT('test_mul_001')) suite.addTest(TestMT('test_mul_002')) suite.addTest(TestMT('test_div_001')) suite.addTest(TestMT('test_div_002')) suite.addTest(TestMT('test_square_001')) suite.addTest(TestMT('test_square_002')) with open('test_result.html','wb+') as file: runner = HTMLTestRunner.HTMLTestRunner( stream = file, verbosity = 2 ) runner.run(suite) if __name__ == '__main__': run_test_01() # run_test_02() # run_test_03() 测试报告: