接口测试demo

我们的产品可以通过telnet建立通信,来调试和测试接口

为了提高测试效率,写了一个脚本,从EXCEL里提取测试命令集合,然后逐条通过telnet通道发送到Radio上,收集ACK,再保存到EXCEL里,形成一个初步的报告。

 

实现如下:

 

#!/usr/bin/env python 
# -*- coding: utf-8 -*- 

################################################################################
# @script name   send interface cmd to telnet port and get results in csvlog
# @version               v1
# @date                  2015-10-15
# @author                Yunpeng
# @draft                 Yunpeng
# 
################################################################################

import sys
import telnetlib
import argparse
import time
import xlrd

def readconfig(configfile):
    paraD = {}
    parameter = []
    value = []
    #	Load file with config parameters
    file1 = open(configfile, 'r')
    try:
		lines1 = file1.readlines()
    finally:
        file1.close()
    
    for line in lines1:
        linedata = line.split("=")
        key = linedata[0]
        val = linedata[1].strip()
        parameter.append(key)
        value.append(val)
        #print ('Get parameter : %s = %s' % (key,val))
        paraD.setdefault(key,val)
    #print paraD
    return paraD

def readCmd(xlsfile):
    temp = ""
    pool = []
    Workbook = xlrd.open_workbook(xlsfile)
    worksheet = Workbook.sheet_by_name("FRMON")
    
    global nrows
    nrows = worksheet.nrows
    print "there are %s rows in xls" % nrows
    ncols = worksheet.ncols
    print "there are %s cols in xls" % ncols
    
    for i in range(1,nrows):
        rowdata = worksheet.row_values(i)
        #print rowdata
        for i in range(len(rowdata)):
            rowdata[i] = str(rowdata[i])
        pool.append(rowdata)
    return pool

def creatEmptyCSV(name):
    s = "\n"
    f = open(name,'w')
    f.write(s)
    f.close()

def list2strWithComma(list):
    for i in range (len(list)):
        list[i] = str(list[i])
    a = ",".join(list)
    return a
    
def addline2csv(origcsv,Datalist):
    "new data need be a list(one grade)"
    csvfile = open(origcsv,'a+')
    new = list2strWithComma(Datalist)
    newline = new + "\n"
    csvfile.write(newline)
    csvfile.close()    

def maketimestamp():
    timestamp = time.strftime('%Y-%m-%d_%H-%M-%S',time.localtime(time.time()))
    return timestamp
    
def sendCmdSet(cmdset):
    #	Open telent connection to RFM
    ack = []
    match=[]
    resultAll=[]
    try:
        tn = telnetlib.Telnet(frmonIP_1, port, TIMEOUT)
        print "connect to %s done!" % frmonIP_1
    except:
        try:
            tn = telnetlib.Telnet(frmonIP_2, port)
        except:	
            print 'Unable to connect'
            time.sleep(10)
            sys.exit(0)
    
    for i in range(len(cmdset)):
        tag = cmdset[i][0]
        line = cmdset[i][1]
        expectAck0 = cmdset[i][2]
        expectAck = expectAck0.strip()
        
        if tag == "sleep":
            sec = float(line)
            print "========== wait for %s seconds ===========" % sec
            time.sleep(sec)
            ack.append("sleep for %s seconds" % str(sec))
            match.append("-")
            continue
        else:
            cmd = str(line)
            #print "==> FRMON input: [%s]" % cmd
            tn.write(cmd+"\n")
            ra = tn.read_until("==>")
            rb = tn.read_until(">")
            r = ra + rb
            r0 = r.strip()
            r1 = r0.strip(">")
            r2 = r1.strip()
            #print "<== FRMON output: [%s]" % r2
            ack.append(r2)
            if r2 == expectAck :
                match.append("Match")
            else:
                match.append("Not match")
        time.sleep(1)
    tn.close()
    resultAll.append(ack)
    resultAll.append(match)
    return resultAll
          
def saveCsvlog(cmdset,resultAll,logtag):
    firstline = ["Tag","Frmon_Cmd","Expect_Ack","Real_Ack","Do it Match Expect?"]
    timestat = maketimestamp()
    logName = logtag + "_" + timestat + ".csv"
    creatEmptyCSV(logName)
    addline2csv(logName,firstline)
    for i in range(len(cmdset)):
        line = []
        line = cmdset[i]
        line.append(resultAll[0][i])
        line.append(resultAll[1][i])
        addline2csv(logName,line)

configfile = "config.ini"
xlsfile = "cmd.xls"
parameter = readconfig(configfile)
logTag = parameter["logtagname"]
frmonIP_1 = parameter["frmonIP_1"]
frmonIP_2 = parameter["frmonIP_2"]
port = int(parameter["port"])
TIMEOUT = int(parameter["timeout"])
        
cmdpool = readCmd(xlsfile)
ack = sendCmdSet(cmdpool)
saveCsvlog(cmdpool,ack,logTag)


当然,里面对于EXCEL命令集合的格式是定义好的,必须是文件名为“cmd.xls”的EXCEL文件。

 

内容必须是如下的格式,

 

为了实现更复杂的操作,预留了一列标签tag列。可以编辑定义不同的标签,来实现扩展的功能,我只加了一个sleep标签,实现遇到sleep延迟发送下一条消息几秒的功能。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值