Bloggers have added a log system to their interface Automation framework today
Based on the Python logging library, including the log main function, generate log file:
1 #-*-coding:utf-8-*-2 #Log System3 #Time: 2017-08-314 #Name: xx5 6 ImportLogging7 ImportOS8 fromDatetimeImportdatetime9 Ten One classMainlog: A def __init__(self): - Pass - the @staticmethod - defGetLog (): - Logging.basicconfig ( - #Filename=mainlog.logtxt (), # Log output to file +Level=logging. DEBUG, -format="% (asctime) s-% (levelname) s-% (message) s", +datefmt="%y-%m-%d%h:%m:%s", A ) at returnLogging - - @staticmethod - deflogtxt (): -Today_name = Datetime.today (). Strftime ("%y%m%d") -Log_name = Datetime.today (). Strftime ("%y%m%d%h%m%s") +". txt" in #folder path for New Day -Path ="f:\\logs\\"+ Today_name +"\\" to ifos.path.exists (path): + Pass - Else: the os.makedirs (path) * #log file path name $File_path = path +Log_namePanax Notoginseng returnFile_path
1 #-*-coding:utf_8-*-2 #Basic Interface Request Method3 #Time: 2017-08-314 ImportRequests5 fromRequests.exceptionsImportreadtimeout, Connectionerror, Requestexception6 fromLog.main_logImportMainlog7 8 9 classMainresponse:Ten def __init__(self): One Pass A - @staticmethod - defMainpost (Url=none, Params=none, json=None): theU""" - URL: The URL of the interface request - params: request header for Interface request - JSON: JSON for interface requests + """ -Logger =Mainlog.getlog () + Try: AResponse = Requests.post (Url=url, Params=params, Json=json, timeout=10) atCode =Response.status_code -Text =Response.text -Logger.debug (U"Interface Request Address:") - logger.debug (URL) -Logger.debug (U"interface Request Header:") - Logger.debug (params) inLogger.debug (U"Interface Request Parameters:") - Logger.debug (JSON) toLogger.debug (U"Interface Request return code:") + logger.debug (code) -Logger.debug (U"interface return information:") the logger.debug (text) * returntext $ exceptreadtimeout:Panax NotoginsengLogger.error (U"Request timed out") - exceptConnectionerror: theLogger.error (U"Request Connection Error") + exceptrequestexception: ALogger.error (U"return error")
Then add the log format to the POST request in your package
The debug level is normal content.
Error level is the wrong scenario
Finally, execute the automation use case in the UnitTest framework and generate the test report:
Python Learning Notes (log system implementation)