1、小练习:定义三个方法(加法、减法、断言),通过使用关键字驱动测试这个三个方法
compute.py
#encoding=utf-8 def add(a,b): print a + b return a + b def sub(a,b): print a - b return a - b def assert_value(a,b): if a == b: return True else: return False
testdata.txt文件
add,1,2,3
sub,2,1,1
test_compute.py
#encoding=utf-8 from compute import * test_case_num = 0 success_case_num = 0 faile_case_num = 0 with open("D:\PythonProject\MyTest\testdata.txt") as fp: for line in fp: test_case_num += 1 action,data1,data2,expect_result = line.split(',') expect_result = eval(expect_result.strip()) s = action + "(" + data1 + "," + data2 + ")" actual_result = eval(s) if assert_value(actual_result,expect_result): success_case_num += 1 else: faile_case_num += 1 print "total %s test cases ran " % test_case_num print "total %s succeeded test cases ran " % success_case_num print "total %s failed test cases ran " % faile_case_num
上面的函数都是两个参数,如果函数的参数不同,有的一个,有的三个,有的没有,则需要优化拼接eval表达式,如:
compute.py
#encoding=utf-8 import random import re #两个参数 def add(a,b): print a + b return a + b #两个参数 def sub(a,b): print a - b return a - b #三个参数 def mul(a,b,c): print a*b*c return a*b*c #一个参数 def abs_value(a): print abs(a) return abs(a) #无参数,返回值是一个随机,无法使用精确值断言,所以使用正则来断言 def random_value(): print random.randint(1,1000) return random.randint(1,1000) def assert_value(actual_result,expected_result): #将真实结果、期望结果强制转换为字符串类型 actual_result = str(actual_result) expected_result = str(expected_result) #判断期望结果是否包含正则表达式 if re.search(r"\|*|?",expected_result): #如果期望结果是正则表达式,则使用期望结果对真实结果进行匹配 if re.match(expected_result,actual_result): return True else: return False #如果期望结果不是正则表达式,则直接断言是否相等 if actual_result == expected_result: return True else: return False
testdata.txt
add,1,2,3
sub,2,1,1
mul,1,2,3,6
abs_value,-1,1
random_value,d+
测试数据的第一行的第一个为函数名,最后一个为断言值,所以中间的均为参数,需要对切割开的数据进行判断长度,再取出切片(去头去尾,即只留参数),最后拼接成一个函数表达式。如:mul(1,2,3)
test_comput.py
#encoding=utf-8 from compute import * test_case_num = 0 success_case_num = 0 faile_case_num = 0 with open("D:\PythonProject\MyTest\testdata.txt") as fp: for line in fp: test_case_num += 1 #切割出函数名 func_name = line.split(",")[0].strip() #切割出参数再拼接成一个字符串 values = ",".join(line.split(",")[1:-1]).strip() #切割出断言值 expected_result = line.split(",")[-1].strip() #拼接成执行函数的表达式 s = func_name + "(" + values + ")" #使用eval执行表达式,并将结果保存到actual_result actual_result = eval(s) #进行断言 if assert_value(actual_result,expected_result): success_case_num += 1 else: faile_case_num += 1 print "total %s test cases ran " % test_case_num print "total %s succeeded test cases ran " % success_case_num print "total %s failed test cases ran " % faile_case_num #生成html测试报告 content =Template(""" <html> <head><title>光荣之路测试报告</title> <meta charset="utf-8" /> </head> <table border="6"> <caption>测试报告</caption> <tr> <td>运行的总用例数</td> <td>成功运行的总用例数</td> <td>失败运行的总用例数</td> </tr> <tr> <td>$num1</td> <td>$num2</td> <td>$num3</td> </tr> </table> </html> """) file_content = content.substitute(num1=test_case_num,num2=success_test_case,num3=faile_test_case ) with open("result.html","w") as f: f.write(file_content)