Learn about the python logging module logging, you can refer to the following blog, write a very detailed
https://www.cnblogs.com/yyds/p/6901864.html
https://www.cnblogs.com/goodhacker/p/3355660.html
https://cuiqingcai.com/6080.html
Practice: The log module is added to the interface requests + excel testing framework
Create a folder logs, a log file for storing the output; logger.py in utils then create a tool, packaging method call log
logger.py reads as follows
# - * - Coding: UTF-. 8 - * - Import the logging Import OS from utils Import getcwd log_path, = os.path.dirname (getcwd.get_cwd ()) Print (log_path,) class Logger: DEF the __init __ (Self, loggerName): # create a Logger self.logger = logging.getLogger (loggerName) self.logger.setLevel (logging.DEBUG) # create a handler, for writing a log file log_path = os.path.dirname (getcwd.get_cwd ()) + " / logs / "# specify the output file path, note logs that folder, be sure to add /, otherwise it will lead to error output path, the logs become part of the file name logname = log_path + 'out.log' # specify the output the log file name fh = logging.FileHandler (logname, encoding = 'utf-8') # specify utf-8 format coding, preventing the log garbled text output fh.setLevel (logging.DEBUG) # Create a handler, for outputting the log to the console CH = logging.StreamHandler () ch.setLevel (logging.DEBUG) # handler defined format output formatter = logging.Formatter ( '% (asctime ) s -% (name ) S -% (levelname) S -% (Message) S ') fh.setFormatter (Formatter) ch.setFormatter (Formatter) # Add to the logger Handler self.logger.addHandler (FH) self.logger.addHandler (CH) DEF get_log (Self):
"" "defines a function callback logger of" "" return self.logger IF the __name__ == '__main__': . T = Logger ( "HMK") get_log () Debug ( "% S IS the User. loging "% 'jeck')
Logs used in the main function run_main.py
# coding: utf-8 # author: hmk from base.main import RunMethod from get_datas.get_data import GetData from utils.handle_excel import HandleExcel import json from utils.logger import Logger class RunMain: def __init__(self): """实例化写好的类,备用""" self.run_method = RunMethod() self.get_data = GetData() self.excel = HandleExcel() self.logger = Logger(__name__) def run(self): count = self.get_data.get_nrows() # print(count) for i in range(1, count): priority = self.get_data.get_priority(i) # 获取用例的优先级priority url = self.get_data.get_url (i) # of cases acquired url with method = self.get_data.get_method (i) # acquisition request method params = self.get_data.get_params (i) # acquisition parameters (data taken out here is string format) params_load = json.loads (the params) to the acquired parameter # deserialization, i.e., into the dictionary except_value = self.get_data.get_except_value (i) # obtain the expected result value IF priority == 'H': R & lt = self.run_method.run_main (method, url, params_load , header = None) # If use case is a high priority, the execution # Print (R & lt) self.logger.get_log (). Debug ( 'first' + str (i) + 'to return a result of interfaces:% s', r) # output interface in response to the content # Print (type (R & lt)) IF in R & lt except_value: # Print (self.get_data.get_caseSeq (I) + 'test passed') self.logger.get_log (). debug ( 'first' + str (i) + 'interface test through') # self.get_data.write_values (I, 'the PASS') # call get_data.py file write_values () method self.excel.write_value (i, 11, 'pass ') # call handle_excel.py file write_value () method of the else: # Print (self.get_data.get_caseSeq (I) + 'failed') self.logger.get_log () .debug ( 'first' + str (i) + 'interface test does not pass through') # self.get_data.write_values (I, 'FAIL') self.excel.write_value (I,. 11, 'Fail') IF the __name__ == '__main__': T = RunMain () t.run ()
Print out the contents of the log
So that you can replace with print output to view the content of the code to run