日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

request,logging,ConfigParser——接口框架

發布時間:2025/5/22 49 豆豆
生活随笔 收集整理的這篇文章主要介紹了 request,logging,ConfigParser——接口框架 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

做一個將參數和用例分開放置,并且輸出log的接口測試框架

我的框架如下所示

Log文件用來設置log輸出文件,需要時可以在用例內調用輸出,config用來填寫一切需要的參數信息,jiekou_post_test是我用來寫接口測試用例的文件,log是自動輸出的log文件,readConfig是讀取congfig參數的執行文件

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用來存放參數

readConfig

# encoding=utf-8
import os

import codecs

import ConfigParser

proDir = os.path.split(os.path.realpath(__file__))[0]#os.path.realpath(__file__),獲取當前執行腳本的絕對路徑

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。根據每個字節的開頭的固定格式,我們就可以判斷是否是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'三里屯辦公區' 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如下

?

轉載于:https://www.cnblogs.com/garvicker/p/10233516.html

總結

以上是生活随笔為你收集整理的request,logging,ConfigParser——接口框架的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。