Python在HTTP接口测试中的应用

上一篇 / 下一篇  2012-07-10 15:02:09 / 个人分类:Python

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软件测试网 B S8F^C~;Px-v
作者:xiaonan51Testing软件测试网0Dmf,~I:buqN
输入参数:待转换的字符串数据51Testing软件测试网^P8A s @
输出参数:转换完成后的字符串数据51Testing软件测试网-zs}T E%Kv
'''51Testing软件测试网YB*~9B Y BG
def urlcode(data):
#\(a f4\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~ kv~X8M
'''
E)n2f;q JA*T0def GetUserInfo(uname,method):51Testing软件测试网 e7p+t9^5F/`"~n+i
    if method == 'GET':
~)Fd a pp @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 El l0        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-Aw x8v
        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@:T b0import xlrd
N/H:?1^Ja'CHt0'''
函数说明: Testcase 脚本
作者:xiaonan
输入参数:测试数据,API接口
输出参数:测试日志,测试报告
'''
9{4D.~3W(fS8c@VnY0def 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#\&d G9x6\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软件测试网Y5qN"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:t0M wXd3D
            WriterLog(...) #写测试日志
a/d^*t3Y+v$s]5V0            WriterReport(...)#写测试报告51Testing软件测试网 Cs{C5Q GM
        else51Testing软件测试网qRg*o@*Nh e
            WriterLog(...)#写测试日志
UE?0o5IA_0            WriterReport(...)#写测试报告
51Testing软件测试网3|J(\g!uT Xqy"\
3. 组织测试套,用驱动文件去调用执行所有测试套件,完成相关测试,并生成测试日志及测试报告.51Testing软件测试网z\6I;IZ6TW
   # -*- coding:gb2312 -*-51Testing软件测试网 H `N2nAkz9^!K
   ''' 51Testing软件测试网j4^E'?8I5kP~
   函数说明: Testsuit Driver驱动脚本 
   作者:xiaonan
   输入参数:TestCase 脚本
   输出参数:测试日志,测试报告
51Testing软件测试网 ~ k mK;_/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\
       ...
;C g+sF LL0       Report(....) # 统计汇总所有测试报告数据,以文件或页面形式呈现.
4. 执行测试脚本,分析测试结果. 根据测试报告,如果有Bug则提交.

TAG:

jacobsong的个人空间 引用 删除 jacobsong   /   2015-10-14 12:44:37
5
huangxl_huangxl的个人空间 引用 删除 huangxl_huangxl   /   2014-03-25 15:40:19
可惜太粗略了
huangxl_huangxl的个人空间 引用 删除 huangxl_huangxl   /   2014-03-25 15:40:05
3
 

评分:0

我来说两句

Open Toolbar