HTTP接口例子:http:\\ip:port\interface.php?uname=aaa51Testing软件测试网
^6C:MNw0{7\7I
接口功能: 根据uname参数值来返回对应的用户名的基本信息
51Testing软件测试网J3? R9F
f,[1. 用Python封装被测试接口,对于HTTP接口我们通常会采用 GET和POST 2种调用方式去访问,所以必须把这2种方式都封装进去 # -*- coding:gb2312 -*-
(wl?|#}
T0import urllib2,urllib
'''51Testing软件测试网&]r/~*S"m*f
函数说明:url 特殊字符编码转换51Testing软件测试网
BS8F^C~;Px-v
作者:xiaonan51Testing软件测试网0Dmf,~I:buqN
输入参数:待转换的字符串数据51Testing软件测试网^P8As
@
输出参数:转换完成后的字符串数据51Testing软件测试网-zs}T E%K v
'''51Testing软件测试网YB*~9BYBG
def urlcode(data):
#\(af4\L'q(|d0 return urllib2.quote(str(data))
51Testing软件测试网N){@7u6E0ua_/a
'''
)`A*j7HX/LB1\F0函数说明:获取用户信息的API接口51Testing软件测试网_u]0^O)d
作者:xiaonan51Testing软件测试网G{)Qg+K)h&u
输入参数:用户名(uname),HTTP接口调用方式(GET或者POST)
r&Y)hnQ%d1v;sC1s0输出参数:HTTP接口调用返回数据51Testing软件测试网D0G-j2~
k v~X8M
'''
E)n2f;qJA*T0def GetUserInfo(uname,method):51Testing软件测试网e7p+t9^5F/`"~n+i
if method == 'GET':
~)Fd
a
p p @0?p.o0 url = 'http://ip:port/interface/GetUserInfo.php?uname='+urlcode(uname)
v-jN4Jtl'Uf8n0 result = urllib2.urlopen(url).read()51Testing软件测试网qp1lJ` E
return result
(pdp1gis0
'[ ?!rs;z}0 if method == 'POST':
swf7P5u Ell0 url = 'http://ip:port/interface/GetUserInfo.php'
XG2QD ^
N{Q0 values = {'uname' : uname}51Testing软件测试网8xmw8?,rQ
data = urllib.urlencode(values)51Testing软件测试网cP'Z[5Plt
req = urllib2.Request(url, data)
3x$b5o&z$T0 response = urllib2.urlopen(req)
!~p9G2DI3k0 result = response.read()51Testing软件测试网{M{(i2pj-A wx8v
return result
2. 编写、组织测试脚本, 准备测试数据51Testing软件测试网H'L$A#m i~%D
根据Testcase的具体业务逻辑用事先准备好的测试数据去调用封装好的API接口,验证实际返回结果是否与预期返回结果一致.
测试数据可以以各种形式存放,如Excel数据表:
2g_
mcc.{(s
h0_0 TestCaseName uname method Expected Result
/bkoF;i0 TestCase1 aaaa GET ....51Testing软件测试网u
h.N C)gy}]
TestCase2 aaaa POST ....51Testing软件测试网`%Wh.H!x+oG?6Z:Vf
TestCase3 bbbb GET ....51Testing软件测试网(zO2Y&Ti~
... ... ... ....
# -*- coding:gb2312 -*-
\)N)x6}B7r{T@:Tb0import xlrd
N/H:?1^Ja'CHt0'''
函数说明: Testcase 脚本
作者:xiaonan
输入参数:测试数据,API接口
'''
9{4D.~3W(fS8c@ Vn Y0def GetUser():51Testing软件测试网$x,y$`-v:c6E
bk = xlrd.open_workbook(excel文件名称) # 打开excel文件
,^,_}3jd#o,}@&`0 sh = bk.sheet_by_name(excel表名)# 打开excel表
O&V^5G6ic"dTS0 nrows = sh.nrows # 获取总行数
@+_3{*y
p&S&@{0 for i in range(1,nrows): 51Testing软件测试网k6{8|L4a4c"^
TestCase = sh.cell_value(i,0)
+^7V6C#\&dG9x6\0 uname = sh.cell_value(i,1)
D
h#e+f$SSa1\0 method = sh.cell_value(i,2)51Testing软件测试网iLi.z4}M
D
EX_Result=sh.cell_value(i,3)51Testing软件测试网Y5q N"Md{s5\8fu
WriterLog('Testcase Name:'+TestCase+'TestData: uname = '+uname+' ,method = '+method+' ,EX_Result = ' + ,EX_Result) # 写测试日志51Testing软件测试网$u5Fy@X+E
i
AC_result = GetUserInfo(uname,method) # 调用API接口
"i@cbr4G`a(F't0 WriterLog('AC_result = ' + AC_result) # 写测试日志51Testing软件测试网&{A^R$R&MSe{
if EX_Result == AC_result: #实际结果与预期结果对比51Testing软件测试网5y!@B:t0MwXd3D
WriterLog(...) #写测试日志
a/d^*t3Y+v$s]5V0 WriterReport(...)#写测试报告51Testing软件测试网C s{C5QGM
else51Testing软件测试网qRg*o@*Nhe
WriterLog(...)#写测试日志
UE?0o5IA_0 WriterReport(...)#写测试报告
51Testing软件测试网3|J(\g!uTXqy"\
3. 组织测试套,用驱动文件去调用执行所有测试套件,完成相关测试,并生成测试日志及测试报告.51Testing软件测试网z\6I;I Z6TW
# -*- coding:gb2312 -*-51Testing软件测试网H`N2n Akz9^!K
''' 51Testing软件测试网j4^E'?8I5kP~
函数说明: Testsuit Driver驱动脚本
作者:xiaonan
输入参数:TestCase 脚本
输出参数:测试日志,测试报告
51Testing软件测试网
~kmK;_/E '''
51Testing软件测试网~k
X){0@6T3B7@*O` if __name__ == '__main__':
3|!kD'F$TH0 ...
51Testing软件测试网(a:gr5x
@,^V WriterLog() #写测试日志
51Testing软件测试网'Hhl+l;TlUe GetUser() # TestCase 脚本
51Testing软件测试网MgzV[Py ...
51Testing软件测试网z1e Pr-|j:N2\ ...
;Cg+sF
LL0 Report(....) # 统计汇总所有测试报告数据,以文件或页面形式呈现.
4. 执行测试脚本,分析测试结果. 根据测试报告,如果有Bug则提交.