• 20180925-4单元测试


    此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2146

    结对伙伴:孙韦男

    代码地址:https://git.coding.net/doubanjiang73/two.git

    测试用例

    功能一  

        名称:生成四则运算题目

        操作步骤:从coding下载四则运算代码在控制台中运行。首先输入 python -m unitest 文件名 之后按照程序提示写出对应答案,程序给出结果。

        预期结果:可以生成四则运算题目并能输出结果。

    功能二 

        名称:生成带有括号的四则运算题目

        操作步骤:从coding下载四则运算代码在控制台中运行。首先输入 python -m unitest 文件名 之后按照程序提示写出对应答案,程序给出结果。

        预期结果:可以生成带有括号的四则运算题目并能输出结果。

    功能三

        名称:生成带小数四则运算题目并支持“精美输出”

        操作步骤:从coding下载四则运算代码在控制台中运行。首先输入 python -m unitest 文件名 之后按照程序提示写出对应答案,程序给出结果。

        预期结果:可以生成带有小数的四则运算题目并能进行“精美输出”。

    功能四

        名称:能输出带分数的四则运算题目,并支持分数约分

        操作步骤:从coding下载四则运算代码在控制台中运行。首先输入 python -m unitest 文件名 之后按照程序提示写出对应答案,程序给出结果。

        预期结果:可以生成带有分数的四则运算题目并能输出结果。

        在最开始时我们对于单元测试的理解有误认为只需要能运行便是正确的,于是我们最开始写的单元测试只是简单的调用程序看是否出结果,写出了以下一段代码。

    print('-----开始测试-----
    ')
    print('请输入f4开始测试功能1')
    import gongneng1
    print('
    -----功能1测试结束-----')
    print('请输入f4开始测试功能2')
    import gongneng2
    print('
    -----功能2测试结束-----')
    print('请输入f4 -c x (x为要显示题目的数量)开始测试功能3')
    import gongneng3
    print('
    -----功能3测试结束-----')
    print('请输入f4 -c x (x为要显示题目的数量)开始测试功能4')
    import gongneng4
    print('
    -----功能4测试结束-----')
    print('
    -----所有测试结束-----')
    
     

        但在观看往届同学的单元测试发现,单元测试是指对软件中的最小可测试单元进行检查和验证并非是运行整个程序。在学习宋雨同学的单元测试博文后,我们再次尝试进行单元测试。

    功能1的单元测试代码:

    import unittest
    from gongneng1 import *
    
    class TestDict(unittest.TestCase):
    
        def test_calc(self):
            print('-----开始测试-----
    ')
            str = ra_number()
            print(str)
            result = input('输入正确结果
    >')
            self.assertEqual(int(result), my_eval(str))
            print('
    -----测试结束-----')
    
    if __name__ == '__main__':
        unittest.main()

     功能一运行结果

           但在单元测试的过程中发现只有功能一的代码经过整理后可以进行测试,其余代码在按照单元测试的步骤整理后仍并不能进行测试(以下代码为整理过后的代码)。

    四则运算代码:

    功能一

    import random 
    ops = ['+','-','*','/']
    com = input('>') #用户输入
    cot = 0 #答对的题
    x = 0
    while x < 20 :
        s1 = random.randint(1,10)
        s2 = random.randint(1,10)
        s3 = random.randint(1,10)
        s4 = random.randint(1,10)
        op1 = random.choice(ops) #随机运算符
        op2 = random.choice(ops)
        op3 = random.choice(ops)
        while op1 == op2 == op3:
            op1 = random.choice(ops) #随机运算符
            op2 = random.choice(ops)
            op3 = random.choice(ops)
        eq = (str(s1)+op1+str(s2)+op2+str(s3)+op3+str(s4))
        res = eval(eq) 
        if len(str(res) ) > 5:
            continue
        x += 1
        print(eq)
        in_res =eval( input('?'))
        if in_res == res:
            print('算对啦,你真是个天才!')
            cot += 1
        else:
            print('再想想吧,答案似乎是%s喔!'%res)
    
    print('你一共答对%s道题,共20道题'%cot)

    功能二

    from random import randint
    from random import choice
    ops = ['+','-','*','/']
    bra = ['(', '', ')']
    com = input('>') #用户输入
    cot = 0 #答对的题
    x = 0
    while x < 20 :
        s1 = randint(1,10)
        s2 = randint(1,10)
        s3 = randint(1,10)
        s4 = randint(1,10)
        op1 = choice(ops) #随机运算符
        op2 = choice(ops)
        op3 = choice(ops)
        """括号"""
        bra_1 = ['' , '' ,'']
        bra_2 = ['' , '' , '']
        i = ii =0
        while (i ==0 and ii ==2) or abs(i-ii)==1 
            or ii < i  :
            i = randint(0,2)
            ii = randint(0,2)
    
        bra_1[i] = '(';   bra_2[ii]=')'
    
        while op1 == op2 == op3 :
            op1 = choice(ops) #随机运算符
            op2 = choice(ops)
            op3 = choice(ops)
    
        eq = bra_1[0] + str(s1) + op1 + bra_1[1] + str(s2) + 
        bra_2[0] + op2 + bra_1[2] + str(s3) + bra_2[1] + op3 
        + str(s4) + bra_2[2]
        res = eval(eq) 
        if len(str(res) ) > 5:
            continue
        x += 1
        print(eq)
        in_res =eval( input('?'))
        if in_res == res:
            print('算对啦,你真是个天才!')
            cot += 1
        else:
            print('再想想吧,答案似乎是%s喔!'%res)
    
    print('你一共答对%s道题,共20道题'%cot)

    功能三

    from random import randint
    from random import choice
    import os 
    ops = ['+','-','*','/']
    bra = ['(', '', ')']
    com = input('>') #用户输入
    com_list = com.split()
    while com_list[2].isdigit() == False:
        print('题目数量必须是正整数')
        com = input('>') #用户输入
        com_list = com.split()
    
    def xx():
        s1 = randint(1,10)
        s2 = randint(1,10)
        s3 = randint(1,10)
        s4 = randint(1,10)
        op1 = choice(ops) #随机运算符
        op2 = choice(ops)
        op3 = choice(ops)
        """括号"""
        bra_1 = ['' , '' ,'']
        bra_2 = ['' , '' , '']
        i = ii =0
        while (i ==0 and ii ==2) or abs(i-ii)==1 
            or ii < i  :
            i = randint(0,2)
            ii = randint(0,2)
    
        bra_1[i] = '(';   bra_2[ii]=')'
    
        while op1 == op2 == op3 :
            op1 = choice(ops) #随机运算符
            op2 = choice(ops)
            op3 = choice(ops)
    
        eq = bra_1[0] + str(s1) + op1 + bra_1[1] + str(s2) + 
        bra_2[0] + op2 + bra_1[2] + str(s3) + bra_2[1] + op3 
        + str(s4) + bra_2[2]
        res = eval(eq) 
        return [eq,res]
    
    eq = [];  res = []
    while len(res) < int(com_list[2]):
        a = xx()
        if a[1] in res or len((str(a[1])) ) >6:
            continue
        eq.append(a[0])
        res.append(a[1])
    
    f= open('题目.txt','w')
    for i in range(len(eq)):
        print('{0:15}'.format(eq[i]),end = '')
        print(res[i])
        xxx = 17 - len(eq[i])
        f.write(str(eq[i]+' '*xxx))
        f.write(str(res[i])+'
    ')
    f.close()
    os.system('题目.txt')  #决定是否打开txt

    功能四

    import os
    from random import randint
    from random import choice
    from fractions import Fraction
    ops = ['+','-','*','/']
    bra = ['(', '', ')']
    com = input('>') #用户输入
    com_list = com.split()
    while com_list[2].isdigit() == False:
        print('题目数量必须是正整数')
        com = input('>') #用户输入
        com_list = com.split()
    
    def xx():
        s1 = randint(1,10)
        s2 = randint(1,10)
        s3 = randint(1,10)
        s4 = randint(1,10)
        op1 = choice(ops) #随机运算符
        op2 = choice(ops)
        op3 = choice(ops)
        """括号"""
        bra_1 = ['' , '' ,'']
        bra_2 = ['' , '' , '']
        i = ii =0
        while (i ==0 and ii ==2) or abs(i-ii)==1 
            or ii < i  :
            i = randint(0,2)
            ii = randint(0,2)
    
        bra_1[i] = '(';   bra_2[ii]=')'
    
        while op1 == op2 == op3 :
            op1 = choice(ops) #随机运算符
            op2 = choice(ops)
            op3 = choice(ops)
    
        eq = bra_1[0] + str(s1) + op1 + bra_1[1] + str(s2) + 
        bra_2[0] + op2 + bra_1[2] + str(s3) + bra_2[1] + op3 
        + str(s4) + bra_2[2]
        res = Fraction(eval(eq)) 
        return [eq,res]
    
    
    eq = [];  res = []
    while len(res) < int(com_list[2]):
        a = xx()
        if a[1] in res or len((str(a[1])) ) >6:
            continue
        if int(a[1]) == a[1]: #保证输入全为分数
            continue
        eq.append(a[0])
        res.append(a[1])
    
    f= open('题目.txt','w')
    for i in range(len(eq)):
        print('{0:15}'.format(eq[i]),end = '')
        print(res[i])
        xxx = 17 - len(eq[i])
        f.write(str(eq[i]+' '*xxx))
        f.write(str(res[i])+'
    ')
    f.close()
    os.system('题目.txt')  #决定是否打开txt

     总结:

           通过此次作业让我知道了单元测试是为了测试每一个小模块是否可以正常运行,通常是测试一个程序的子程序或函数。进行单元测试的过程十分艰难,我们也未能完成全部任务要求但这个过程让我与孙韦男同学收获颇丰。

  • 相关阅读:
    【翻译】ASP.NET MVC深度接触:ASP.NET MVC请求生命周期
    水木年华亲笔签名《ASP.NET第一步》等你来拿!!
    《C#与.NET 3.0高级程序设计(特别版)》横空出世
    是什么让你萌发了跳槽的念头?
    (原创)无废话C#设计模式之十八:Command
    (原创)无废话C#设计模式之十六:State
    (原创)无废话C#设计模式之二十:Mediator
    推荐文章索引
    技术图书非常难写
    【翻译】创建IQUERYABLE提供器系列文章
  • 原文地址:https://www.cnblogs.com/zwqhh/p/9742398.html
Copyright © 2020-2023  润新知