日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪(fǎng)問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

request,logging,ConfigParser——接口框架

發(fā)布時(shí)間:2025/5/22 编程问答 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 request,logging,ConfigParser——接口框架 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

做一個(gè)將參數(shù)和用例分開(kāi)放置,并且輸出log的接口測(cè)試框架

我的框架如下所示

Log文件用來(lái)設(shè)置log輸出文件,需要時(shí)可以在用例內(nèi)調(diào)用輸出,config用來(lái)填寫(xiě)一切需要的參數(shù)信息,jiekou_post_test是我用來(lái)寫(xiě)接口測(cè)試用例的文件,log是自動(dòng)輸出的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用來(lái)存放參數(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í)行腳本的絕對(duì)路徑

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ù)每個(gè)字節(jié)的開(kāi)頭的固定格式,我們就可以判斷是否是UTF8的編碼

      data = data[3:]

    file = codecs.open(configPath, "w")#直接用編碼打開(kāi),防止open打開(kāi)的編碼不一致報(bào)錯(cuò)問(wèn)題

    file.write(data)

    file.close()

    fd.close()

    self.cf = ConfigParser.RawConfigParser()#配置文件的格式是: []包含的叫section, section 下有option=value,可以直接get(section,option)來(lái)獲取value

    self.cf.read(configPath)

  def get_http(self, name):

    value = self.cf.get("HTTP", name)

    return value

?

測(cè)試用例文檔如下,用try輸入一個(gè)不存在的IP,這樣抓取錯(cuò)誤寫(xiě)到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))#亂寫(xiě)的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——接口框架的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。