20180925-4 单元测试,结对
此作业要求参见:【https://edu.cnblogs.com/campus/nenu/2018fall/homework/2146】
结对伙伴:祝玮琦
通过学习编写的单元测试的代码:(参考了上一届宋雨的单元测试代码,因为能力有限,只写出来针对功能1的单元测试代码)
功能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()
运行结果如下:
因为是先写完的四则运算程序,然后写的单元测试代码。所以一次通过了。经过规范的四则运算代码如下:
功能1:
import random def ra_number(): ops = ['+', '-', '*', '/'] 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) return str(s1)+op1+str(s2)+op2+str(s3)+op3+str(s4) def my_eval(str): return eval(str) if __name__ == '__main__': com = input('>') # 用户输入 cot = 0 # 答对的题 x = 0 while x < 20: eq = ra_number() res = my_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)
功能2:
from random import randint from random import choice ops = ['+','-','*','/'] bra = ['(', '', ')'] com = input('>') #用户输入 cot = 0 #答对的题 x = 0 def bra(): """括号""" 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]=')' return bra_1,bra_2 def ra_number(): 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) while op1 == op2 == op3 : op1 = choice(ops) #随机运算符 op2 = choice(ops) op3 = choice(ops) (bra_1,bra_2) = bra() return 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] while x < 20 : eq = ra_number() 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)
功能3:
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 bra(): """括号""" 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]=')' return bra_1,bra_2 def ra_number(): 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) while op1 == op2 == op3 : op1 = choice(ops) #随机运算符 op2 = choice(ops) op3 = choice(ops) bra_1,bra_2 = bra() 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] def p_topic(eq,res): 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() eq = []; res = [] while len(res) < int(com_list[2]): a = ra_number() if a[1] in res or len((str(a[1])) ) >6: #结果一样的直接就不要 continue eq.append(a[0]) res.append(a[1]) p_topic(eq,res) os.system('题目.txt') #决定是否打开txt
功能4:
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 ra_bra(): """括号""" 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]=')' return bra_1,bra_2 def ra_number(): 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) while op1 == op2 == op3 : op1 = choice(ops) #随机运算符 op2 = choice(ops) op3 = choice(ops) bra_1,bra_2 = ra_bra() 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] def p_topic(eq,res): 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() eq = []; res = [] while len(res) < int(com_list[2]): a = ra_number() 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]) p_topic(eq,res) os.system('题目.txt') #决定是否打开txt
体会:单元测试能让我们更加规范的书写代码,可以提高代码的质量。