zoukankan      html  css  js  c++  java
  • 第三周作业(4)——单元测试

    单元测试作业地址:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2146

    一、内容

            单元测试是用来对一个模块、一个函数或者一个类来进行正确性检验的测试工作。本次四则运算的实现使用了python语言,对于python的单元测试,我们参考了下述链接:https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/00143191629979802b566644aa84656b50cd484ec4a7838000

    编写单元测试时,只需要引入Python自带的unittest模块。

    二、单元测试代码

     1 # -*- coding: utf-8 -*-
     2 import unittest
     3 from f4 import *
     4 
     5 
     6 class F4Test(unittest.TestCase):
     7     def test_f4(self):
     8         pass
     9 
    10     def test01_create_equation(self):           # 测试顺序按函数名字字典顺序进行
    11         print("create_equation函数单元测试开始:")
    12         self.assertIsNotNone(create_equation())
    13         print("OK")
    14         print("create_equation函数单元测试结束。
    ")
    15 
    16     def test02_reverse_polish(self):
    17         eq = []
    18         print("reverse_polish函数单元测试开始:")
    19         equation = input("输入一个四则运算(括号请使用英文版的括号):")
    20         _eq_ans = input("输入正确的逆波兰表达式:")
    21         list(equation)          # 输入的表达式是str类型,该函数处理的是含有整型和字符型的list类型
    22         for temp in equation:
    23             if '0' <= temp <= '9':
    24                 eq.append(int(temp))
    25             else:
    26                 eq.append(temp)
    27         re_equation = reverse_polish(eq)
    28         str_equation = "".join('%s' % id for id in re_equation)
    29         self.assertEqual(_eq_ans, str_equation)
    30         print("OK")
    31         print("reverse_polish函数单元测试结束。
    ")
    32 
    33     def test03_calculate(self):
    34         eq = []
    35         print("calculate函数单元测试开始:")
    36         equation = input("输入一个可计算的逆波兰表达式:")
    37         _eq_ans = input("输入该表达式的正确结果:")
    38         list(equation)  # 输入的表达式是str类型,该函数处理的是含有整型和字符型的list类型
    39         for temp in equation:
    40             if '0' <= temp <= '9':
    41                 eq.append(int(temp))
    42             else:
    43                 eq.append(temp)
    44         result = calculate(eq)
    45         self.assertEqual(float(_eq_ans), result)
    46         print("OK")
    47         print("calculate函数单元测试结束。
    ")
    48 
    49 
    50 if __name__ == "__main__":
    51     unittest.main()
    单元测试

    三、测试结果

    对生成算式、将算式转为逆波兰表达式、计算算式结果3个函数设计如下单元测试:

    测试1(表达式不带括号):

    测试2(表达式包含括号):

     测试结果均正确。

    四、关键代码

    1.随机生成四则运算式:

     1 def create_equation():      # 随机生成算式
     2     eq = []
     3 
     4     for i in range(3):
     5         eq.append(random.randint(0, 10))
     6         eq.append(operator[random.randint(0, 3)])
     7     eq.append(random.randint(0, 10))
     8     p = random.randint(1, 5)
     9     if p is 1:
    10         eq.insert(0, "(")
    11         eq.insert(4, ")")
    12     elif p is 2:
    13         eq.insert(0, "(")
    14         eq.insert(6, ")")
    15     elif p is 3:
    16         eq.insert(2, "(")
    17         eq.insert(6, ")")
    18     elif p is 4:
    19         eq.insert(2, "(")
    20         eq.append(")")
    21     elif p is 5:
    22         eq.insert(4, "(")
    23         eq.append(")")
    24     return eq
    create_equation

    2.将算式转化为逆波兰表达式

     1 def reverse_polish(equation):       # 将算式转换为逆波兰表达式
     2     result = []
     3     c = []
     4     slist = [i for i in equation]
     5 
     6     for item in slist:
     7         if item in range(0, 100):
     8             result.append(item)
     9         elif not c and item in cal.keys():
    10             c.append(item)
    11             continue
    12         elif c and item in cal.keys():
    13             for x in range(c.__len__()):
    14                 z = c[-1]
    15                 temp = cal[z] if z in cal else cal1[z]
    16                 if temp >= cal[item]:
    17                     result.append(c.pop())
    18                 else:
    19                     c.append(item)
    20                     break
    21             if not c:
    22                 c.append(item)
    23         elif item is ")":
    24             for x in range(c.__len__()):
    25                 if c[-1] == "(":
    26                     c.pop()
    27                     break
    28                 else:
    29                     result.append(c.pop())
    30         elif item is "(":
    31             c.append(item)
    32         # print(result,c)
    33     for x in range(c.__len__()):
    34         result.append(c.pop())
    35     return result
    reverse_polish

    3.计算逆波兰表达式

     1 def calculate(re_equation):         # 计算逆波兰表达式
     2     stack = PyStack()
     3     sumEnd = 0
     4 
     5     if len(re_equation) is 0:
     6         return sumEnd
     7     for i in re_equation:
     8         if i in range(0, 100):
     9             stack.push(float(i))
    10         elif '+' is i:
    11             a = stack.pop()
    12             b = stack.pop()
    13             stack.push(b + a)
    14         elif '-' is i:
    15             a = stack.pop()
    16             b = stack.pop()
    17             stack.push(b - a)
    18         elif '*' is i:
    19             a = stack.pop()
    20             b = stack.pop()
    21             stack.push(b * a)
    22         elif '÷' is i:
    23             a = stack.pop()
    24             b = stack.pop()
    25             if a == 0:
    26                 return False             #print('%d/%d分子不能为0' % (b, a))
    27             else:
    28                 stack.push(b / a)
    29     return stack.pop()
    calculate

    五、版本控制

    git地址:https://git.coding.net/liu-xin/f4.git

  • 相关阅读:
    SSAS没有注册类别 (异常来自 HRESULT:0x80040154 (REGDB_E_CLASSNOTREG)) 解决办法
    Javascript中暂停功能的实现
    【转】JQuery ajax json 实例
    SqlDataAdapter有关InsertCommand,UpdateCommand,DeleteCommand 实例
    绑定数组对象DataTable.Select返回值DataRow[]
    SQL SERVER 联想函数重写
    JQuery Dialog(转)
    温习C 文件操作
    轻松掌握Windows窗体间的数据交互
    反射方法调用时的一个错误:参数计数不匹配( parameter count mismatch )
  • 原文地址:https://www.cnblogs.com/liu-xin1995/p/9746213.html
Copyright © 2011-2022 走看看