數據驅動
定義:程序和數據分離,測試數據存入一個文件中,腳本存入另一個文件中
數據文件路徑:e:\\pythonexcise\\aaa.txt
文件內容:-----以下內容請放到一個txt文本中
add||1||1||2
add||2||2||4
sub||1||1||0
sub||2||1||0
代碼實現
#數據驅動模型
import traceback
#測試數據
with open("e:\\pythonexcise\\aaa.txt") as fp:
data = fp.readlines()
#被測試對象
def add(a,b):
return a+b
def sub(a,b):
return a-b
#測試程序:
for line in data:
#split根據||切割,每一個值賦值給對應的變量
func_name,value1,value2,expected_result = line.strip().split("||")
test_data= "%s(%s,%s)" %(func_name,value1,value2)
print("關鍵字的數據:",test_data)
actual_result = eval(test_data)#計算一個表達式的值並返回結果。
try:
assert actual_result == int(expected_result)
print("測試執行成功,測試數據是%s,%s,%s" %(func_name,value1,value2))
except AssertionError:
traceback.print_exc()
print("當前測試用例執行失敗,測試數據是%s,%s,%s" %(func_name,value1,value2))
print("期望的結果%s,實際的結果%s" %(expected_result,actual_result))
# 關鍵字的數據: add(1,1)
# 測試執行成功,測試數據是add,1,1
# 關鍵字的數據: add(2,2)
# 測試執行成功,測試數據是add,2,2
# 關鍵字的數據: sub(1,1)
# 測試執行成功,測試數據是sub,1,1
# 關鍵字的數據: sub(2,1)
# Traceback (most recent call last):
# File "0716-data.py", line 24, in <module>
# assert actual_result == int(expected_result)
# AssertionError
# 當前測試用例執行失敗,測試數據是sub,2,1
# 期望的結果0,實際的結果1
關鍵字驅動
定義:將測試用例的執行步驟存放在文件中,每個步驟單獨封閉成一個函數,以這個函數名作為關鍵字,將函數名及傳參寫入文件中,第個步驟對應一行文件
數據文件路徑:e:\\pythonexcise\\aaa.txt
文件內容:-----以下內容請放到一個txt文本中
add||1||1||2
add||2||2||4
sub||1||1||0
sub||2||1||0
代碼實現
# 關鍵字驅動
import traceback
#測試數據
with open("e:\\pythonexcise\\aaa.txt") as fp:
data=fp.readlines()
#被測試對像
def add(a,b):
return a+b
def sub(a,b):
return a-b
def execute_test_step(func_name,value1,value2):
test_data="%s(%s,%s)"%(func_name,value1,value2)
print("關鍵字的數據:",test_data)
actual_result=eval(test_data)
return actual_result
def get_test_data(test_data_file_path):
test_data=[]
with open(test_data_file_path) as fp:
for i in fp.readline():
param1,param2,expect_result=i.strip().split("||")
test_data.append(int(param1),int(param2),int(expect_result))
return test_data #[(4,2,2),(1,2,0)]
def assert_result(actual_result,expected_result):
try:
assert actual_result==int(except_result)
print("測試執行成功,測試數據是%s,%s,%s"%(func_name,value1,value2))
except AssertionError:
traceback.print_exc()
print("當前測試用例失敗,測試數據是%s,%s,%s"%(func_name,value1,value2))
print("期望的結果%s,實際的結果%s"%(expected_result,actual_result))
#測試程序:
for line in data:
if len(line.strip().split("||"))==4:
func_name,value1,value2,except_result=line.strip().split("||")
actual_result=execute_test_step(func_name,value1,value2)
assert_result(actual_result,except_result)
elif len(line.strip().split("||"))==2:
func_name,data_file_path=line.strip().split("||")
test_data=get_test_data(data_file_path)
for data in test_data:
actual_result=execute_test_step(func_name,data[0],data[2])
assert_result(actual_result,except_result)
# E:\pythonExcise>python 0714PO.py
# 關鍵字的數據: add(1,1)
# 測試執行成功,測試數據是add,1,1
# 關鍵字的數據: add(2,2)
# 測試執行成功,測試數據是add,2,2
# 關鍵字的數據: sub(1,1)
# 測試執行成功,測試數據是sub,1,1
# 關鍵字的數據: sub(2,1)
# Traceback (most recent call last):
# File "0714PO.py", line 36, in assert_result
# assert actual_result==int(except_result)
# AssertionError
# 當前測試用例失敗,測試數據是sub,2,1
# 期望的結果0,實際的結果1
