• 20190919-4 单元测试,结对


    作业要求参见:https://edu.cnblogs.com/campus/nenu/2019fall/homework/7629

    git地址为:https://e.coding.net/thiking/si_ze_yun_suan.git

    结对伙伴:孙晓宇

    使用语言:Python

    测试框架:UnitTest

    测试内容: 四则运算式子生成及运算结果

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

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

    2、单元测试代码

    import unittest
    from f4_1 import creat_formula,creat_equations
    from f4 import out_input,creat_equation,run_count
    class UnitTest(unittest.TestCase):
        def test01_create_equation(self):       #功能二create_equation函数生成式子测试
            print("create_equation函数单元测试开始:
    ")
            print("create_equation函数随机产生一个式子:")
            result = creat_equation()
            print(result)
            self.assertIsNotNone(creat_equation())
            print("式子产生运算通过")
            print("create_equation函数单元测试结束。
    ")
        def test02_creat_formula(self):           #功能四creat_formula函数生成式子测试
            print("creat_formula函数单元测试开始:
    ")
            print("creat_formula函数随机产生一个式子:")
            result = creat_formula()
            print(result)
            self.assertIsNotNone(creat_formula())
            print("式子产生运算通过")
            print("creat_formula函数单元测试结束。
    ")
        def test03_creat_equations(self):      #功能一creat_equations函数生成式子测试
            print("creat_equations函数单元测试开始:
    ")
            print("creat_equations函数随机产生一个式子:")
            result = creat_equations()
            print(result)
            self.assertIsNotNone(creat_equations())
            print("式子产生运算通过")
            print("creat_equations函数单元测试结束。
    ")
        def test04_run_count(self):  #测试运算结果
            print("run_count函数和单元测试开始:")
            result = creat_equation()
            print("create_equation函数随机产生一个式子:")
            print(result)
            right = int(run_count(result))
            print("请输入你的计算结果:")
            get_input = input()
            get_input = int(get_input)
            print(get_input)
            print("式子的计算结果为:")
            print(right)
            self.assertEqual(right,get_input)
            print("通过测试")
            print("run_count函数单元测试结束:
    ")
    if __name__=='__main__':
        unittest.main()

    测试一(功能一不带括号产生式子)

     测试二(功能二产生带括号的例子)

    测试三(功能四产生分数式子)

    测试四(run_count函数运算结果)

    测试结果均为正确,欢迎使用更多测试用例进行测试

    被测函数分别为:

    creat_equations函数 

    def creat_equations():    #功能一生成随机式子
            ops = ['+', '-', '*', '/']
            num1 = r(1, 9)      #产生随机数
            num2 = r(1, 9)
            num3 = r(1, 9)
            num4 = r(1, 9)
            ops1 = r(0, 2)
            ops2 = r(0, 3)
            ops3 = r(0, 3)
            equa = str(num1) + ops[ops1] + str(num2) + 
             ops[ops2] + str(num3) + 
             ops[ops3] + str(num4)
            return (equa)

    creat_equation函数

    def creat_equation():    #生成随机式子值
        ops = ['+', '-', '*', '/']
        num1 = r(1, 9)      #产生随机数
        num2 = r(1, 9)
        num3 = r(1, 9)
        num4 = r(1, 9)
        ops1 = r(0, 3)
        ops2 = r(0, 3)
        ops3 = r(0, 3)
        dic = {1: '(' + str(num1) + ops[ops1] + '(' + str(num2) + ops[ops2] + str(num3) + ')' + ')' + ops[ops3] + str(num4),
               2: '(' + str(num1) + ops[ops1] + str(num2) + ')' + ops[ops2] + str(num3) + ops[ops3] + str(num4),
               3: str(num1) + ops[ops1] + str(num2) + ops[ops2] + '(' + str(num3) + ops[ops3] + str(num4) + ')',
               4: str(num1) + ops[ops1] + '(' + '(' + str(num2) + ops[ops2] + str(num3) + ')' + ops[ops3] + str(num4) + ')',
               5: '(' + '(' + str(num1) + ops[ops1] + str(num2) + ')' + ops[ops2] + str(num3) + ')' + ops[ops3] + str(num4),
               6: str(num1) + ops[ops1] + '(' + str(num2) + ops[ops2] + '(' + str(num3) + ops[ops3] + str(num4) + ')' + ')',
               7: str(num1) + ops[ops1] + '(' + str(num2) + ops[ops2] + str(num3) + ops[ops3] + str(num4) + ')',
               8: '(' + str(num1) + ops[ops1] + str(num2) + ops[ops2] + str(num3) + ')' + ops[ops3] + str(num4),
               9: '(' + str(num1) + ops[ops1] + str(num2) + ')' + ops[ops2] + '(' + str(num3) + ops[ops3] + str(num4) + ')',
               10: str(num1) + ops[ops1] + '(' + str(num2) + ops[ops2] + str(num3) + ')' + ops[ops3] + str(num4),}
        test = r(1, 10)
        eq = dic[test]
        return (eq)

    creat_formula函数

    def creat_formula():    #功能四生成随机式子
         ops = ['+', '-', '*', '/']
         num1 = r(1, 9)      #产生随机数
         num2 = r(1, 9)
         num3 = r(1, 9)
         num4 = r(1,9)
         ops1 = r(0, 2)
         ops2 = r(0, 3)
         ops3 = r(0, 3)
         dic  = {
                1:str(f(r(1,9),r(1,9))) + ops[ops1] + str(f(r(1,9),r(1,9))) + ops[ops2] + str(num2) + ops[ops3] + str(num3),
                2:str(f(r(1,9),r(1,9))) + ops[ops1] + str(num1) + ops[ops2] + str(f(r(1,9),r(1,9))) +ops[ops3] + str(num3),
                3: str(f(r(1, 9), r(1, 9))) + ops[ops1] + str(num1) + ops[ops2] + str(f(r(1, 9), r(1, 9))) + ops[ops3] + str(f(r(1, 9), r(1, 9))),
                4: str(f(r(1, 9), r(1, 9))) + ops[ops1] + str(num1) + ops[ops2] + str(num2) + ops[ops3] + str(num3),
                5:str(f(r(1,9),r(1,9))) + ops[ops1] + str(num1) + ops[ops2] + str(num2)+ops[ops3] + str(f(r(1,9),r(1,9))) ,
                6:str(num1) + ops[ops1] + str(num2) + ops[ops2] + str(num3) + ops[ops3] + str(num4),
                7:str(f(r(1,9),r(1,9))) + ops[ops1] + str(f(r(1,9),r(1,9))) + ops[ops2] + str(f(r(1,9),r(1,9))) + ops[ops3] +str(num3),
                8: str(f(r(1, 9), r(1, 9))) + ops[ops1] +str(f(r(1,9),r(1,9))) + ops[ops2] + str(num2) + ops[ops3] + str(f(r(1,9),r(1,9))),
                9: str(f(r(1, 9), r(1, 9))) + ops[ops1] + str(f(r(1,9),r(1,9))) + ops[ops2] + str(f(r(1,9),r(1,9))) +ops[ops3] + str(f(r(1,9),r(1,9))),
                10: str(num1) + ops[ops1] + str(num2) + ops[ops2] + str(f(r(1, 9), r(1, 9))) + ops[ops3] + str(f(r(1, 9), r(1, 9))),
                11: str(num1) + ops[ops1] + str(f(r(1,9),r(1,9))) + ops[ops2] + str(f(r(1,9),r(1,9))) + ops[ops3] + str(f(r(1,9),r(1,9))),
                12: str(num1) + ops[ops1] + str(f(r(1,9),r(1,9))) + ops[ops2] + str(f(r(1,9),r(1,9))) + ops[ops3] + str(num3),
                13: str(num1) + ops[ops1] + str(f(r(1,9),r(1,9))) + ops[ops2] + str(num2) +ops[ops3] + str(f(r(1,9),r(1,9))),
                14: str(num1) + ops[ops1] + str(num2) + ops[ops2] + str(num2) + ops[ops3] + str(f(r(1, 9), r(1, 9))),
                15: str(num1) + ops[ops1] + str(f(r(1,9),r(1,9))) + ops[ops2] + str(num2) +ops[ops3] + str(num3),
                16: str(num1) + ops[ops1] + str(num2) + ops[ops2] + str(f(r(1,9),r(1,9))) +ops[ops3] + str(num3),
    
           }
         eq = dic[r(1, 16)]
         return (eq)

    run_count函数

    def run_count(equa):                #计算式子值函数
         result = f(eval(equa)).limit_denominator(1000)   #利用eval函数计算式子的值,并将小数转化为分数
         result = str(result)
         return(result)
  • 相关阅读:
    Exadata 上关于SAS盘的小秘密
    Python 全栈开发:python循环语句while
    Python 全栈开发:python条件语句if..else..
    Python 全栈开发:python基础
    Python 全栈开发:python初识
    计算机基础(简单了解)
    如何计算网络配置中广播域和冲突域的数目?
    使VS开发的程序在Win7系统运行时自动提升权限
    在程序中通过Process启动外部exe的方法及注意事项
    获取指定目录下多种格式的文件
  • 原文地址:https://www.cnblogs.com/dongyahui/p/11586579.html
Copyright © 2020-2023  润新知