最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

一個(gè)Python最簡(jiǎn)單的接口自動(dòng)化框架

 更新時(shí)間:2018年01月02日 11:39:36   作者:沐童  
這篇文章主要為大家詳細(xì)介紹了一個(gè)Python最簡(jiǎn)單的接口自動(dòng)化框架,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

故事背景

讀取一個(gè)Excel中的一條數(shù)據(jù)用例,請(qǐng)求接口,然后返回結(jié)果并反填到excel中。過(guò)程中會(huì)生成請(qǐng)求回來(lái)的文本,當(dāng)然還會(huì)生成一個(gè)xml文件。具體的excel文件如下:

代碼方案

# -*- coding: UTF-8 -*- 
from xml.dom import minidom
import xlrd
import openpyxl
import requests
import json
import sys
import HTMLParser
import os
import re
import codecs
import time
import datetime

reload(sys)
sys.setdefaultencoding('utf-8')

class OptionExcelData(object):
  """對(duì)Excel進(jìn)行操作,包括讀取請(qǐng)求參數(shù),和填寫(xiě)操作結(jié)果"""
  def __init__(self, excelFile,excelPath=''):
    self.excelFile = excelFile
    self.excelPath = excelPath
    self.caseList = []

  """
  傳入:傳入用例Excel名稱(chēng)
  返回:[],其中元素為{},每個(gè){}包含行號(hào)、城市、國(guó)家和期望結(jié)果的鍵值對(duì)
  """
  def getCaseList(self,excelFile,excelPath=''):
    readExcel = xlrd.open_workbook(fileName)              #讀取指定的Excel
    try:
      table = readExcel.sheet_by_index(0)               #獲取Excel的第一個(gè)sheet
      trows = table.nrows                       #獲取Excel的行數(shù)
      for n in range(1,trows):
        tmpdict = {}                        #把一行記錄寫(xiě)進(jìn)一個(gè){}
        tmpdict['id'] = n                      #n是Excel中的第n行
        tmpdict['CityName'] = table.cell(n,2).value
        tmpdict['CountryName'] = table.cell(n,3).value
        tmpdict['Rspect'] = table.cell(n,4).value
        self.caseList.append(tmpdict)
    except Exception, e:
      raise
    finally:
      pass
    return self.caseList

  """
  傳入:請(qǐng)求指定字段結(jié)果,是否通過(guò),響應(yīng)時(shí)間
  返回:
  """
  def writeCaseResult(self,resultBody,isSuccess,respTime,\
    excelFile,theRow,theCol=5):
    writeExcel = openpyxl.load_workbook(excelFile)           #加載Excel,后續(xù)寫(xiě)操作
    try:
      wtable = writeExcel.get_sheet_by_name('Sheet1')         #獲取名為Sheet1的sheet
      wtable.cell(row=theRow+1,column=theCol+1).value = resultBody  #填寫(xiě)實(shí)際值
      wtable.cell(row=theRow+1,column=theCol+2).value = isSuccess   #填寫(xiě)是否通過(guò)
      wtable.cell(row=theRow+1,column=theCol+3).value = respTime   #填寫(xiě)響應(yīng)時(shí)間
      writeExcel.save(excelFile)
    except Exception, e:
      raise
    finally:
      pass


class GetWeather(object):
  """獲取天氣的http請(qǐng)求"""
  def __init__(self, serviceUrl,requestBody,headers):
    self.serviceUrl = serviceUrl
    self.requestBody = requestBody
    self.headers = headers
    self.requestResult = {}

  """
  傳入:請(qǐng)求地址,請(qǐng)求體,請(qǐng)求頭
  返回:返回{},包含響應(yīng)時(shí)間和請(qǐng)求結(jié)果的鍵值對(duì)
  """
  def getWeath(self,serviceUrl,requestBody,headers):
    timebefore = time.time()                      #獲取請(qǐng)求開(kāi)始的時(shí)間,不太嚴(yán)禁
    tmp = requests.post(serviceUrl,data=requestBody,\
      headers=headers)
    timeend = time.time()                        #獲取請(qǐng)求結(jié)束的時(shí)間
    tmptext = tmp.text
    self.requestResult['text'] = tmptext                #記錄響應(yīng)回來(lái)的內(nèi)容
    self.requestResult['time'] = round(timeend - timebefore,2)     #計(jì)算響應(yīng)時(shí)間
    return self.requestResult

class XmlReader:
  """操作XML文件"""
  def __init__(self,testFile,testFilePath=''):
    self.fromXml = testFile
    self.xmlFilePath = testFilePath
    self.resultList = []

  def writeXmlData(self,resultBody,testFile,testFilePath=''):
    tmpXmlFile = codecs.open(testFile,'w','utf-16')           #新建xml文件
    tmpLogFile = codecs.open(testFile+'.log','w','utf-16')       #新建log文件

    tmp1 = re.compile(r'\<.*?\>')                   #生成正則表達(dá)式:<*?>
    tmp2 = tmp1.sub('',resultBody['text'])               #替換相應(yīng)結(jié)果中的<*?>
    html_parser = HTMLParser.HTMLParser()
    xmlText = html_parser.unescape(tmp2)                #轉(zhuǎn)換html編碼

    try:
      tmpXmlFile.writelines(xmlText.strip())             #去除空行并寫(xiě)入xml
      tmpLogFile.writelines('time: '+\
        str(resultBody['time'])+'\r\n')               #把響應(yīng)時(shí)間寫(xiě)入log
      tmpLogFile.writelines('text: '+resultBody['text'].strip())   #把請(qǐng)求回來(lái)的文本寫(xiě)入log
    except Exception, e:
      raise
    finally:
      tmpXmlFile.close()
      tmpLogFile.close()

  """返回一個(gè)list"""
  def readXmlData(self,testFile,testFilePath=''):
    tmpXmlFile = minidom.parse(testFile)
    root = tmpXmlFile.documentElement
    tmpValue = root.getElementsByTagName('Status')[0].\
    childNodes[0].data
    return tmpValue                           #獲取特定字段并返回結(jié)果,此處選取Status


if __name__ == '__main__':

  requesturl = 'http://www.webservicex.net/globalweather.asmx/GetWeather'
  requestHeadrs = {"Content-Type":"application/x-www-form-urlencoded"}
  fileName = u'用例內(nèi)容.xlsx'

  ed = OptionExcelData(fileName) 
  testCaseList = ed.getCaseList(ed.excelFile)

  for caseDict in testCaseList:
    caseId = caseDict['id']
    cityName = caseDict['CityName']
    countryName = caseDict['CountryName']
    rspect = caseDict['Rspect']
    requestBody = 'CityName='+cityName+'&CountryName='+countryName

    getWeather = GetWeather(requesturl,requestBody,requestHeadrs)
    #獲取請(qǐng)求結(jié)果
    tmpString = getWeather.getWeath(getWeather.serviceUrl,\
      getWeather.requestBody,getWeather.headers)

    xd = XmlReader(str(caseId) + '.xml')
    #把請(qǐng)求內(nèi)容寫(xiě)入xml和log
    xd.writeXmlData(tmpString,xd.fromXml)
    response = xd.readXmlData(str(caseId) + '.xml')
    respTime = tmpString['time']
    if response == rspect:
      theResult = 'Pass'
    else:
      theResult = 'False'
   ed.writeCaseResult(response,theResult,respTime,fileName,caseId)

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • SpringBoot實(shí)現(xiàn)登錄注冊(cè)常見(jiàn)問(wèn)題解決方案

    SpringBoot實(shí)現(xiàn)登錄注冊(cè)常見(jiàn)問(wèn)題解決方案

    這篇文章主要介紹了SpringBoot實(shí)現(xiàn)登錄注冊(cè)常見(jiàn)問(wèn)題解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-03-03
  • wxPython定時(shí)器wx.Timer簡(jiǎn)單應(yīng)用實(shí)例

    wxPython定時(shí)器wx.Timer簡(jiǎn)單應(yīng)用實(shí)例

    這篇文章主要介紹了wxPython定時(shí)器wx.Timer簡(jiǎn)單應(yīng)用,實(shí)例分析了Python使用wxPython創(chuàng)建窗口應(yīng)用程序及定時(shí)器的相關(guān)使用技巧,需要的朋友可以參考下
    2015-06-06
  • python正則表達(dá)式re模塊詳細(xì)介紹

    python正則表達(dá)式re模塊詳細(xì)介紹

    這篇文章主要介紹了python正則表達(dá)式re模塊詳細(xì)介紹,本文翻譯自官方文檔,并加入了自己的理解,需要的朋友可以參考下
    2014-05-05
  • Python 實(shí)現(xiàn)數(shù)組相減示例

    Python 實(shí)現(xiàn)數(shù)組相減示例

    今天小編就為大家分享一篇Python 實(shí)現(xiàn)數(shù)組相減示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-12-12
  • python 獲取字典鍵值對(duì)的實(shí)現(xiàn)

    python 獲取字典鍵值對(duì)的實(shí)現(xiàn)

    這篇文章主要介紹了python 獲取字典鍵值對(duì)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • python 插入日期數(shù)據(jù)到Oracle實(shí)例

    python 插入日期數(shù)據(jù)到Oracle實(shí)例

    這篇文章主要介紹了python 插入日期數(shù)據(jù)到Oracle實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-03-03
  • Python shutil模塊文件和目錄操作示例詳解

    Python shutil模塊文件和目錄操作示例詳解

    本文將會(huì)學(xué)習(xí)到?shutil?模塊,包括其主要功能和示例代碼,以幫助你更好地理解如何使用它來(lái)處理文件和目錄
    2023-11-11
  • pandas groupby 分組取每組的前幾行記錄方法

    pandas groupby 分組取每組的前幾行記錄方法

    下面小編就為大家分享一篇pandas groupby 分組取每組的前幾行記錄方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-04-04
  • Python獲取系統(tǒng)默認(rèn)字符編碼的方法

    Python獲取系統(tǒng)默認(rèn)字符編碼的方法

    這篇文章主要介紹了Python獲取系統(tǒng)默認(rèn)字符編碼的方法,涉及Python中sys模塊getdefaultencoding方法的使用技巧,需要的朋友可以參考下
    2015-06-06
  • Python函數(shù)裝飾器實(shí)現(xiàn)方法詳解

    Python函數(shù)裝飾器實(shí)現(xiàn)方法詳解

    這篇文章主要介紹了Python函數(shù)裝飾器實(shí)現(xiàn)方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Python函數(shù)裝飾器的概念、功能、用法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下
    2018-12-12

最新評(píng)論

娄烦县| 芦溪县| 明星| 灵石县| 南昌市| 兴城市| 彰化市| 阳朔县| 密云县| 枣阳市| 绥江县| 岳阳县| 边坝县| 佛教| 文安县| 舒兰市| 炎陵县| 蓬安县| 赤水市| 临朐县| 桦南县| 北京市| 纳雍县| 福鼎市| 奉贤区| 南投县| 商南县| 晋江市| 建昌县| 海口市| 商南县| 北安市| 柳州市| 银川市| 垣曲县| 湘阴县| 高尔夫| 同德县| 兴山县| 新绛县| 巫溪县|