实现接口自动化测试,仅限于同一类型的接口(此例中都为post)
- 自动读取csv测试用例中的数据,执行测试
- 自动判断接口测试结果,执行断言
- 将接口测试结果,响应结果等内容保存到测试报告中
以下是具体实现代码和测试用例,测试报告范本
import requests,csv
class workflow_interface_v5():
def interface_test(self,url,user_info,result,interface_name):
response = requests.post(url,data = user_info).text
print(response)
msg = response.find(result)
test_report = {}
test_report["测试接口名称"] = str(interface_name)
if msg > 0:
print(interface_name+"测试成功")
test_report["测试结论"] = interface_name+"测试成功"
else:
print(interface_name+"测试失败")
test_report["测试结论"] = interface_name+"测试失败"
test_report["测试响应结果"] = str(response)
return test_report
def save_report(self,test_report):
report_path = "./test_report/workflow_test_report_v52.csv"
report_file = open(report_path,"a")
for key,value in test_report.items():
report_file.write(str(key)+","+str(value)+",")
report_file.write("\n")
report_file.close()
if __name__ == '__main__':
workflow_interface_v5_obj = workflow_interface_v5()
data_path = "./test_data/workflow_test_data.csv"
data_file = open(data_path,"r")
data_table = csv.reader(data_file)
for row in data_table:
url = row[3]
result = row[5]
interface_name = row[1]
user_info = {}
for i in range(7, int(row[6]) * 2 + 7, 2):
user_info[row[i]] = row[i + 1]
test_report = workflow_interface_v5_obj.interface_test(url, user_info,result,interface_name)
workflow_interface_v5_obj.save_report(test_report)
接口测试报告范本
接口测试用例范本