博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python - unitest - 实战题目
阅读量:6189 次
发布时间:2019-06-21

本文共 5177 字,大约阅读时间需要 17 分钟。

''' 题目要求 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() 测试报告:

 

 

转载于:https://www.cnblogs.com/gotesting/p/10026705.html

你可能感兴趣的文章
SpringMVC总结
查看>>
以写代学:python 元组
查看>>
win7 配置jdk
查看>>
Linux文件查找及压缩工具
查看>>
消息队列
查看>>
Android图形显示系统——下层显示1:基础知识与相关文件
查看>>
linux故障解决-centos7删除冲突的包
查看>>
利用Karma、Jasmine 做前端单元测试
查看>>
秒杀场景下MySQL的低效原因和改进
查看>>
spring cloud
查看>>
带你走近AngularJS - 体验指令实例
查看>>
NAT不能上网总结
查看>>
kafka的监控
查看>>
c++中正则表达式(regex)
查看>>
Ubuntu Eclipse 解决中文乱码
查看>>
修复/boot及/etc/fstab、自制linux、编译安装内核
查看>>
我们成长的道路需要鸡汤滋补
查看>>
【避坑】初次接项目的血与泪,扎坑了老铁(二)
查看>>
正则表达式(二)之元字符
查看>>
OCP 12c最新考试原题及答案(071-4)
查看>>