request,logging,ConfigParser——接口框架
做一個將參數(shù)和用例分開放置,并且輸出log的接口測試框架
我的框架如下所示
Log文件用來設(shè)置log輸出文件,需要時可以在用例內(nèi)調(diào)用輸出,config用來填寫一切需要的參數(shù)信息,jiekou_post_test是我用來寫接口測試用例的文件,log是自動輸出的log文件,readConfig是讀取congfig參數(shù)的執(zhí)行文件
Log.py
#encoding=utf-8
import logging
from datetime import datetime
import threading
class myLog:
def __init__(self):
self.logger = logging.getLogger()
self.logger.setLevel(level = logging.INFO)
handler = logging.FileHandler("log.txt")
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
self.logger.addHandler(handler)
?
config用來存放參數(shù)
readConfig
# encoding=utf-8
import os
import codecs
import ConfigParser
proDir = os.path.split(os.path.realpath(__file__))[0]#os.path.realpath(__file__),獲取當(dāng)前執(zhí)行腳本的絕對路徑
configPath = os.path.join(proDir, "config.ini")
class ReadConfig:
def __init__(self):
fd = open(configPath)
data = fd.read()
# remove BOM
if data[:3] == codecs.BOM_UTF8:#判斷是否包含EF BB BF。根據(jù)每個字節(jié)的開頭的固定格式,我們就可以判斷是否是UTF8的編碼
data = data[3:]
file = codecs.open(configPath, "w")#直接用編碼打開,防止open打開的編碼不一致報錯問題
file.write(data)
file.close()
fd.close()
self.cf = ConfigParser.RawConfigParser()#配置文件的格式是: []包含的叫section, section 下有option=value,可以直接get(section,option)來獲取value
self.cf.read(configPath)
def get_http(self, name):
value = self.cf.get("HTTP", name)
return value
?
測試用例文檔如下,用try輸入一個不存在的IP,這樣抓取錯誤寫到log中
#encoding=utf-8
import sys
reload(sys)
sys.path.append('..')
from readConfig import ReadConfig
from common.Log import myLog
import requests
import json,time
#sys.setdefaultencoding("utf-8")
#print ReadConfig().get_http('url')
session = requests.session()
url=ReadConfig().get_http('url')
#url = "http://home.travelsky.net/publish/zghxnw/index.html"
params=ReadConfig().get_http('params')
#headers=ReadConfig().get_http('headers')
r = session.post(url, data=params,verify=False)
if u'今天我生日' in r.text:
print "login success"
url2=ReadConfig().get_http('url2')
#url2="http://home.travelsky.net/publish/zghxnw/847/860/863/index.html"
s = session.get(url2)
if u'三里屯辦公區(qū)' in s.text:
print "link success"
try:
response = requests.get(www.dfsfss.com, timeout=float(timeout))#亂寫的IP
print 'response'
except:
logger=myLog().logger
logger.error("Time out!")
?
log如下
?
轉(zhuǎn)載于:https://www.cnblogs.com/garvicker/p/10233516.html
總結(jié)
以上是生活随笔為你收集整理的request,logging,ConfigParser——接口框架的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 酶是否可以降低活化能来提高反应速率
- 下一篇: python2.x- selenium-