Get the App
SLTechnology News&Howtos  ›  Servers  › 

Use the higher-order functions of Python to write logs and capture abnormal information

Shulou Source: shulou.com Published: 2022-06-02 10:04:18 09月13日 Update

Import logging

Import time

Import traceback

From selenium import webdriver

Def log (test_case_func):

Def wraps (* args, * * keywords):

Log_name= time.strftime ("% Y%m%d%H%M%S", time.localtime ()) + test_case_func.__name__

Logging.basicConfig (level=logging.INFO,filemode='w',filename='./'+log_name+'.log')

Test_time = time.strftime ("% Y-%m-%d% H:%M:%S", time.localtime ())

Logging.info (test_time+': now starts testing:'+ test_case_func.__name__)

Try:

Test_case_func (* args, * * keywords)

Logging.info (test_time+': test completed successfully!' )

Except:

Logging.info (test_time+': test has an exception:'+ traceback.format_exc ())

Return wraps

@ log

Def test_case1 (driver):

Driver.get ('https://www.baidu.com/?tn=sitehao123_15')

Driver.maximize_window ()

Driver.find_element_by_id ('kw11')

If _ _ name__ = ='_ _ main__':

Driver = webdriver.Firefox ()

Test_case1 (driver)

Tags: Testing success information functions logs high-level Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology OPPO Reno Linux Shulou Tech Info macOS