python unittest模块
2018-06-18 00:43:12来源:未知 阅读 ()
1 import unittest 2 import random 3 4 5 class Operation(object): 6 7 def __init__(self, num1=0, num2=0): 8 if not (0 <= num1 <= 10 and 0 <= num2 <= 10): 9 raise OutOfRangeError('number out of range (must be 0~10)') 10 if not isinstance(num1, int) or not isinstance(num2, int): 11 raise NotIntegerError('non‐integers can not be operate') 12 self.operate_num1 = num1 13 self.operate_num2 = num2 14 15 def get_result(self): 16 pass 17 18 19 class AddOp(Operation): 20 21 def get_result(self): 22 return self.operate_num1 + self.operate_num2 23 24 25 class MinusOp(Operation): 26 27 def get_result(self): 28 return self.operate_num1 - self.operate_num2 29 30 31 class MultiOp(Operation): 32 33 def get_result(self): 34 return self.operate_num1 * self.operate_num2 35 36 37 class DivideOp(Operation): 38 39 def get_result(self): 40 return self.operate_num1 / self.operate_num2 41 42 43 class OperationFactory(object): 44 45 @staticmethod 46 def choose_operation(op): 47 if op == '+': 48 return AddOp() 49 elif op == '-': 50 return MinusOp() 51 elif op == '*': 52 return MultiOp() 53 elif op == '/': 54 return DivideOp() 55 56 57 class OutOfRangeError(ValueError): 58 pass 59 60 61 class NotIntegerError(ValueError): 62 pass 63 64 65 class KnownValues(unittest.TestCase): 66 67 def test_add_op(self): 68 """测试加法运算是否正确""" 69 ope_obj = OperationFactory.choose_operation('+') 70 for i in range(0, 11): 71 ope_obj.operate_num1 = i 72 ope_obj.operate_num2 = random.randint(1, 10) 73 sum1 = ope_obj.operate_num1 + ope_obj.operate_num2 74 sum2 = ope_obj.get_result() 75 self.assertEqual(sum1, sum2) 76 77 def test_out_of_range(self): 78 """测试出界""" 79 for i in [-1, 11]: 80 operate_num1 = i 81 self.assertRaises(OutOfRangeError, Operation, operate_num1) 82 83 def test_integer(self): 84 """测试浮点数""" 85 operate_num1 = 0.5 86 self.assertRaises(NotIntegerError, Operation, operate_num1) 87 88 89 if __name__ == '__main__': 90 unittest.main()
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
上一篇:Beautifulsoup模块
下一篇:027class_part1
- python3基础之“术语表(2)” 2019-08-13
- python3 之 字符串编码小结(Unicode、utf-8、gbk、gb2312等 2019-08-13
- Python3安装impala 2019-08-13
- 小白如何入门 Python 爬虫? 2019-08-13
- python_字符串方法 2019-08-13
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash