其他分享
首页 > 其他分享> > 20200924-3 单元测试,结对

20200924-3 单元测试,结对

作者:互联网

此作业要求参见:https://edu.cnblogs.com/campus/nenu/2020Fall/homework/11243

结对伙伴:李超

要求1 对每个功能,先给出测试用例,然后再编码功能。请注意把测试用例视为功能需求完成的检验指标。 (40分)

 测试用例:

                                   

单元测试代码:

测试代码测试了f4的几个基本功能,包括生成随机的表达式,中缀表达式转成后缀表达式,后缀表达式的计算

import unittest
from f4 import *


class F4Test(unittest.TestCase):
    def test_f4(self):
        pass

    # 测试顺序按函数名字字典顺序进行
    def test01_create_formula(self):
        print("create_formula函数单元测试开始:")
        self.assertIsNotNone(create_formula())
        print("OK")
        print("create_formula函数单元测试结束。\n")

    def test02_reverse_polish(self):
        eq = []
        print("reverse_polish函数单元测试开始:")
        equation = input("输入一个四则运算(括号请使用英文版的括号):")
        _eq_ans = input("输入正确的后缀表达式:")
        # 输入的表达式是str类型,该函数处理的是含有整型和字符型的list类型
        list(equation)
        for temp in equation:
            if '0' <= temp <= '9':
                eq.append(int(temp))
            else:
                eq.append(temp)
        re_equation = reverse_polish(eq)
        str_equation = "".join('%s' % id for id in re_equation)
        self.assertEqual(_eq_ans, str_equation)
        print("OK")
        print("reverse_polish函数单元测试结束。\n")

    def test03_calculate(self):
        eq = []
        print("calculate函数单元测试开始:")
        equation = input("输入一个可计算的后缀表达式:")
        _eq_ans = input("输入该表达式的正确结果:")
        list(equation)
        for temp in equation:
            if '0' <= temp <= '9':
                eq.append(int(temp))
            else:
                eq.append(temp)
        result = calculate(eq)
        self.assertEqual(float(_eq_ans), result)
        print("OK")
        print("calculate函数单元测试结束。\n")


if __name__ == "__main__":
    unittest.main()

 测试截图如下:

 

 

 

 

 

 

 

 

 

 生成表达式代码如下:

def create_formula():
    equation = []

    for i in range(3):
        equation.append(random.randint(0, 10))
        equation.append(operator[random.randint(0, 3)])
    equation.append(random.randint(0, 10))
    p = random.randint(1, 5)
    if p is 1:
        equation.insert(0, "(")
        equation.insert(4, ")")
    elif p is 2:
        equation.insert(0, "(")
        equation.insert(6, ")")
    elif p is 3:
        equation.insert(2, "(")
        equation.insert(6, ")")
    elif p is 4:
        equation.insert(2, "(")
        equation.append(")")
    elif p is 5:
        equation.insert(4, "(")
        equation.append(")")
    return equation

中缀转后缀代码如下:

# 中缀表达式转为后缀表达式
def reverse_polish(equation):
    result = []
    c = []
    slist = [i for i in equation]

    for item in slist:
        if item in range(0, 100):
            result.append(item)
        elif not c and item in operator_dic.keys():
            c.append(item)
            continue
        elif c and item in operator_dic.keys():
            for x in range(c.__len__()):
                z = c[-1]
                temp = operator_dic[z] if z in operator_dic else cal1[z]
                if temp >= operator_dic[item]:
                    result.append(c.pop())
                else:
                    c.append(item)
                    break
            if not c:
                c.append(item)
        elif item is ")":
            for x in range(c.__len__()):
                if c[-1] == "(":
                    c.pop()
                    break
                else:
                    result.append(c.pop())
        elif item is "(":
            c.append(item)
    for x in range(c.__len__()):
        result.append(c.pop())
    return result

计算后缀表达式代码如下:

# 计算后缀表达式
def calculate(re_equation):
    stack = Stack()
    sumEnd = 0

    if len(re_equation) is 0:
        return sumEnd
    for i in re_equation:
        if i in range(0, 100):
            stack.push(float(i))
        elif '+' is i:
            a = stack.pop()
            b = stack.pop()
            stack.push(b + a)
        elif '-' is i:
            a = stack.pop()
            b = stack.pop()
            stack.push(b - a)
        elif '*' is i:
            a = stack.pop()
            b = stack.pop()
            stack.push(b * a)
        elif '÷' is i:
            a = stack.pop()
            b = stack.pop()
            if a == 0:
                return False
            else:
                stack.push(b / a)
    return stack.pop()

要求2 在博客报告测试用例全部fail 到 全部pass 的过程,报告事实 (fail到修改代码或者测试用例,到pass) 以及收获。 除了最初的框架,测试用例中存在一次性pass没有经过fail的,也报告一次性通过,给出如此优秀地实现了这部分功能的代码。由2位同学中的一位发布博客提交到作业,指明自己的结对伙伴;另一位在作业中引用这一博客,指明自己的结对伙伴。(40分)

引用结对伙伴李超的博客内容。

要求4 使用coding.net做版本控制。checkin 前要求清理 临时文件、可执行程序,通常执行 build-clean可以达到效果。(5分)

代码地址:https://e.coding.net/wyh1/size/size.githttps://e.coding.net/wyh1/size/size.git

标签:结对,equation,单元测试,elif,pop,item,20200924,stack,append
来源: https://www.cnblogs.com/wangyanhe/p/13776351.html