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

python - unitest - 实战题目的更多相关文章

  1. 向大家介绍我的新书:《基于股票大数据分析的Python入门实战》

    我在公司里做了一段时间Python数据分析和机器学习的工作后,就尝试着写一本Python数据分析方面的书.正好去年有段时间股票题材比较火,就在清华出版社夏老师指导下构思了这本书.在这段特殊时期内,夏老 ...

  2. Python 机器学习实战 —— 监督学习(上)

    前言 近年来AI人工智能成为社会发展趋势,在IT行业引起一波热潮,有关机器学习.深度学习.神经网络等文章多不胜数.从智能家居.自动驾驶.无人机.智能机器人到人造卫星.安防军备,无论是国家级军事设备还是 ...

  3. 【图文详解】python爬虫实战——5分钟做个图片自动下载器

    python爬虫实战——图片自动下载器 之前介绍了那么多基本知识[Python爬虫]入门知识,(没看的先去看!!)大家也估计手痒了.想要实际做个小东西来看看,毕竟: talk is cheap sho ...

  4. Python开发实战教程(8)-向网页提交获取数据

    来这里找志同道合的小伙伴!↑↑↑ Python应用现在如火如荼,应用范围很广.因其效率高开发迅速的优势,快速进入编程语言排行榜前几名.本系列文章致力于可以全面系统的介绍Python语言开发知识和相关知 ...

  5. Python爬虫实战(4):豆瓣小组话题数据采集—动态网页

    1, 引言 注释:上一篇<Python爬虫实战(3):安居客房产经纪人信息采集>,访问的网页是静态网页,有朋友模仿那个实战来采集动态加载豆瓣小组的网页,结果不成功.本篇是针对动态网页的数据 ...

  6. Python爬虫实战(2):爬取京东商品列表

    1,引言 在上一篇<Python爬虫实战:爬取Drupal论坛帖子列表>,爬取了一个用Drupal做的论坛,是静态页面,抓取比较容易,即使直接解析html源文件都可以抓取到需要的内容.相反 ...

  7. Spring MVC 程序首页的设置 - 一号门-程序员的工作,程序员的生活(java,python,delphi实战)

    body { font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI ...

  8. python机器学习实战(一)

    python机器学习实战(一) 版权声明:本文为博主原创文章,转载请指明转载地址 www.cnblogs.com/fydeblog/p/7140974.html  前言 这篇notebook是关于机器 ...

  9. python机器学习实战(二)

    python机器学习实战(二) 版权声明:本文为博主原创文章,转载请指明转载地址 http://www.cnblogs.com/fydeblog/p/7159775.html 前言 这篇noteboo ...

随机推荐

  1. ABAP:parameters的用法

    parameters 1.基础用法 parameters:p0(20) type c. 2.使用DEFAULT后缀为参数指定缺省值. parameters:p1(20) type c default ...

  2. 移动端rem单位和px单位换算

    rem单位是根据html元素的单位在页面根据不同的手机屏幕分辨率动态整体的按比例缩小或放大字体. 假如html{font-size: 14px;},那么1rem=14px; 一个div宽度48px,那 ...

  3. 【css】css2实现两列三列布局的方法

    前言 对于 flex 弹性布局相信大家都有所了解,它是 css3 中的属性,然而它具有一定的兼容性问题.楼主前几天面试时遇到了面试官需要设计一个两列布局,我当然就说父元素 flex 吧哩吧啦,然而需要 ...

  4. <Android 应用 之路> 天气预报(三)

    昨天介绍了基本的载入界面,今天介绍下天气信息显示界面的代码 基本ListView显示 搜索框,查询城市 上一篇文章中,载入界面通过showWeatherInfo()方法跳转到天气信息显示界面 priv ...

  5. css隐藏元素的几种方法与区别

    css隐藏元素的几种方法与区别 一:display:none;隐藏不占位 display 除了不能加入 CSS3 动画豪华大餐之外,基本效果卓越,没什么让人诟病的地方. 二:position:abso ...

  6. windows server 2008 r2 启用 Windows Defender

    单击“开始”,指向“管理工具”,然后单击“服务器管理器”. 在“服务器管理器”中,单击“功能”,然后在“服务器管理器”细节窗格中的“功能摘要”下,单击“添加功能”. 此时会启动“添加功能向导”. 在“ ...

  7. GWT module 'xxx' may need to be (re)compiled解决办法

    使用GWT Eclipse Plug-in开发GWT应用,启动程序,在浏览器地址栏中输入http://127.0.0.1:8888/HelloWorld.html,没有出现我所期望的结果,而是弹出如下 ...

  8. Python正则表达式计算器流程图

  9. JavaScript onkeydown事件入门实例(键盘某个按键被按下)

    JavaScript onkeydown 事件 用户按下一个键盘按键时会触发 onkeydown 事件.与 onkeypress事件不同的是,onkeydown 事件是响应任意键按下的处理(包括功能键 ...

  10. iOS项目工程及目录结构

    做过一些iOS的项目,不同项目的沉淀没有积累到一起,目录的管理都在后期随着人员的增加越来越混乱,因此在这里做一些梳理,希望达到两个目的. 一套相对通用的目录结构,作为后续项目的模版. 积累相应的基础库 ...