使用python,调用公共类,向类中传入参数可以实现任意接口测试
向此类中传入任意参数,即可实现接口测试,可以传入url(接口访问地址),user_info(接口body数据),result(响应结果),interface_name(接口名称,用于打印某某接口测试成功)等等
import requests
import csv
class workflow_forgetpassword_v2():
def user_info(self,url,userinfo,result):
self.url = url
self.user_info = userinfo
response = requests.post(self.url,self.user_info,interface_name).text
print(response)
self.result = result
msg = response.find(self.result)
if msg > 0:
print(interface_name+"接口测试成功")
else:
print(interface_name+"接口测试失败")
if __name__=="__main__":
workflow_forgetpassword_v2_obj = workflow_forgetpassword_v2()
'''
url = "http://192.168.0.5:8080/jwshoplogin/user/login.do"
user_info = {
"username":"李超",
"password":"123123"
}
result = "登录成功"
'''
path = "./test_data/login_test_data.csv"
testdata_file = open(path,"r")
testdata_table = csv.reader(testdata_file)
for row in testdata_table:
url = row[3]
for i in range(7,7+int(row[6])):
user_info ={
"username":row[0],
"password":row[1]
}
result = str(row[5])
interface_name = row[1]
workflow_forgetpassword_v2_obj.user_info(url,user_info,result,interface_name)
但是有一定的要求,接口方法必须一致,这个类中接口方法都是post,需要测试的接口方法也必须都是post