python學習筆記(日誌系統實現)

來源:互聯網
上載者:User

標籤:mat   路徑名   pos   介面自動化   alt   erro   測試報告   com   .com   

 

博主今天在自己的介面自動化架構中添加了日誌系統

基於python內建的logging庫、包括日誌主函數、組建記錄檔檔案:

 1 # -*- coding: utf-8 -*- 2 # 日誌系統 3 # 時間:2017-08-31 4 # 姓名:xx 5  6 import logging 7 import os 8 from datetime import datetime 9 10 11 class MainLog:12     def __init__(self):13         pass14 15     @staticmethod16     def getLog():17         logging.basicConfig(18             # filename=MainLog.logTxt(),    # 日誌輸出到檔案19             level=logging.DEBUG,20             format="%(asctime)s - %(levelname)s - %(message)s",21             datefmt="%Y-%m-%d %H:%M:%S",22         )23         return logging24 25     @staticmethod26     def logTxt():27         today_name = datetime.today().strftime("%Y%m%d")28         log_name = datetime.today().strftime("%Y%m%d%H%M%S") + ".txt"29         # 建立當天的檔案夾路徑30         path = "F:\\Logs\\" + today_name + "\\"31         if os.path.exists(path):32             pass33         else:34             os.makedirs(path)35         # 記錄檔路徑名稱36         file_path = path + log_name37         return file_path
 1 # -*- coding: utf_8 -*- 2 # 基礎介面要求方法 3 # 時間:2017-08-31 4 import requests 5 from requests.exceptions import ReadTimeout, ConnectionError, RequestException 6 from Log.main_log import MainLog 7  8  9 class MainResponse:10     def __init__(self):11         pass12 13     @staticmethod14     def mainPost(url=None, params=None, json=None):15         u"""16         url: 介面請求的url17         params: 介面請求的要求標頭18         json: 介面請求的json19         """20         logger = MainLog.getLog()21         try:22             response = requests.post(url=url, params=params, json=json, timeout=10)23             code = response.status_code24             text = response.text25             logger.debug(u"介面請求地址:")26             logger.debug(url)27             logger.debug(u"介面要求標頭:")28             logger.debug(params)29             logger.debug(u"介面請求參數:")30             logger.debug(json)31             logger.debug(u"介面請求返回碼:")32             logger.debug(code)33             logger.debug(u"介面返回資訊:")34             logger.debug(text)35             return text36         except ReadTimeout:37             logger.error(u"請求逾時")38         except ConnectionError:39             logger.error(u"請求串連錯誤")40         except RequestException:41             logger.error(u"返回錯誤")

然後在自己封裝的post請求中把日誌格式加進去

DEBUG層級的是普通的內容

ERROR層級的是錯誤情境

最後在unittest架構中執行自動化用例、產生測試報告:

 

python學習筆記(日誌系統實現)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.