HTTP接口例子:http:\\ip:port\interface.php?uname=aaa
接口功能: 根据uname参数值来返回对应的用户名的基本信息
接口功能: 根据uname参数值来返回对应的用户名的基本信息
1. 用Python封装被测试接口,对于HTTP接口我们通常会采用 GET和POST 2种调用方式去访问,所以必须把这2种方式都封装进去
# -*- coding:gb2312 -*-
import urllib2,urllib
import urllib2,urllib
'''
函数说明:url 特殊字符编码转换
作者:xiaonan
输入参数:待转换的字符串数据
输出参数:转换完成后的字符串数据
'''
def urlcode(data):
return urllib2.quote(str(data))
函数说明:url 特殊字符编码转换
作者:xiaonan
输入参数:待转换的字符串数据
输出参数:转换完成后的字符串数据
'''
def urlcode(data):
return urllib2.quote(str(data))
'''
函数说明:获取用户信息的API接口
作者:xiaonan
输入参数:用户名(uname),HTTP接口调用方式(GET或者POST)
输出参数:HTTP接口调用返回数据
'''
def GetUserInfo(uname,method):
if method == 'GET':
url = 'http://ip:port/interface/GetUserInfo.php?uname='+urlcode(uname)
result = urllib2.urlopen(url).read()
return result
if method == 'POST':
url = 'http://ip:port/interface/GetUserInfo.php'
values = {'uname' : uname}
data = urllib.urlencode(values)
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
result = response.read()
return result
2. 编写、组织测试脚本, 准备测试数据
根据Testcase的具体业务逻辑用事先准备好的测试数据去调用封装好的API接口,验证实际返回结果是否与预期返回结果一致.
根据Testcase的具体业务逻辑用事先准备好的测试数据去调用封装好的API接口,验证实际返回结果是否与预期返回结果一致.
测试数据可以以各种形式存放,如Excel数据表:
TestCaseName uname method Expected Result
TestCase1 aaaa GET ....
TestCase2 aaaa POST ....
TestCase3 bbbb GET ....
... ... ... ....
TestCaseName uname method Expected Result
TestCase1 aaaa GET ....
TestCase2 aaaa POST ....
TestCase3 bbbb GET ....
... ... ... ....
# -*- coding:gb2312 -*-
import xlrd
'''
import xlrd
'''
函数说明: Testcase 脚本
作者:xiaonan
输入参数:测试数据,API接口
输出参数:测试
日志,测试报告
'''
def GetUser():
bk = xlrd.open_workbook(excel文件名称) # 打开excel文件
sh = bk.sheet_by_name(excel表名)# 打开excel表
nrows = sh.nrows # 获取总行数
for i in range(1,nrows):
TestCase = sh.cell_value(i,0)
uname = sh.cell_value(i,1)
method = sh.cell_value(i,2)
EX_Result=sh.cell_value(i,3)
WriterLog('Testcase Name:'+TestCase+'TestData: uname = '+uname+' ,method = '+method+' ,EX_Result = ' + ,EX_Result) # 写测试日志
AC_result = GetUserInfo(uname,method) # 调用API接口
WriterLog('AC_result = ' + AC_result) # 写测试日志
if EX_Result == AC_result: #实际结果与预期结果对比
WriterLog(...) #写测试日志
WriterReport(...)#写测试报告
else
WriterLog(...)#写测试日志
WriterReport(...)#写测试报告
def GetUser():
bk = xlrd.open_workbook(excel文件名称) # 打开excel文件
sh = bk.sheet_by_name(excel表名)# 打开excel表
nrows = sh.nrows # 获取总行数
for i in range(1,nrows):
TestCase = sh.cell_value(i,0)
uname = sh.cell_value(i,1)
method = sh.cell_value(i,2)
EX_Result=sh.cell_value(i,3)
WriterLog('Testcase Name:'+TestCase+'TestData: uname = '+uname+' ,method = '+method+' ,EX_Result = ' + ,EX_Result) # 写测试日志
AC_result = GetUserInfo(uname,method) # 调用API接口
WriterLog('AC_result = ' + AC_result) # 写测试日志
if EX_Result == AC_result: #实际结果与预期结果对比
WriterLog(...) #写测试日志
WriterReport(...)#写测试报告
else
WriterLog(...)#写测试日志
WriterReport(...)#写测试报告
3. 组织测试套,用驱动文件去调用执行所有测试套件,完成相关测试,并生成测试日志及测试报告.
# -*- coding:gb2312 -*-
'''
函数说明: Testsuit Driver驱动脚本
作者:xiaonan
输入参数:TestCase 脚本
输出参数:测试日志,测试报告
'''
if __name__ == '__main__':
...
WriterLog() #写测试日志
GetUser() # TestCase 脚本
...
...
Report(....) # 统计汇总所有测试报告数据,以文件或页面形式呈现.
4. 执行测试脚本,分析测试结果. 根据测试报告,如果有Bug则提交.