'''
题目要求 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. js 数组array es5-es6+ 新增方法函数

    arr.forEach(function(item,index,arr){},this)  相当于普通的for循环,第一个回调参数,第二个this可以重定向[箭头函数则不生效] arr.map() 非 ...

  2. UNITY_MATRIX_MVP和UnityObjectToClipPos

    在unity5.6以上版本中,shader中的UNITY_MATRIX_MVP将会被UnityObjectToClipPos替代,以后我们在写顶点函数时就是这样的 v2f vert(appdata v ...

  3. android配置android studio not found target android-*.的问题

    列:not found target android-25, 打开下载android SDK的工具栏,找到android-25版本下载到你本地的sdk路径下就OK了.

  4. 多段图动态规划dp

    多段图问题是DP的基础题目.大体的意思是有一个赋权有向图,其顶点集被分为几个子集.求经过每个子集从源点到终点的最短路径 import java.util.ArrayList; import java. ...

  5. JavaScript_3_输出

    1. JavaScript通常用于操作HTML元素,可以使用getElementById(id)方法. JavaScript由Web浏览器来执行. 2. document.write()仅仅向文档输出 ...

  6. SPOJ BALNUM Balanced Numbers 平衡数(数位DP,状压)

    题意: 平衡树定义为“一个整数的某个数位若是奇数,则该奇数必定出现偶数次:偶数位则必须出现奇数次”,比如 222,数位为偶数2,共出现3次,是奇数次,所以合法.给一个区间[L,R],问有多少个平衡数? ...

  7. COGS 147. [USACO Jan08] 架设电话线

    ★★☆   输入文件:phoneline.in   输出文件:phoneline.out   简单对比时间限制:1 s   内存限制:16 MB Farmer John打算将电话线引到自己的农场,但电 ...

  8. Spark性能调优之道——解决Spark数据倾斜(Data Skew)的N种姿势

    原文:http://blog.csdn.net/tanglizhe1105/article/details/51050974 背景 很多使用Spark的朋友很想知道rdd里的元素是怎么存储的,它们占用 ...

  9. python_100_静态方法

    class Dog(object): def __init__(self,name): self.name=name @staticmethod#实际上跟类没什么关系了 def eat():#def ...

  10. How to restrict root user to access or modify a file and directory in Linux

    Now in this article I will show you steps to prevent or restrict access of root user to access certa ...