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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

python实时读取日志并打印关键字怎么实现_python pytest测试框架介绍五---日志实时输出...

發(fā)布時間:2025/3/15 python 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python实时读取日志并打印关键字怎么实现_python pytest测试框架介绍五---日志实时输出... 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

同樣的,在使用pytest進行自動化測試時,需要將實時日志打印出來,而不是跑完后才在報告中出結(jié)果。

不過,好在pytest在3.3版本開始,就支持這一功能了,而不用再像nose一樣,再去裝第三方插件。

網(wǎng)上也有相關(guān)實時的日志輸入說明,但我嘗試后,不是我想要的,比如:pytest輸出Log

看看我們下面這樣一段代碼,以unittest模式寫的:

#coding:utf-8

'''Created on 2017年8月31日

@author: huzq'''

from __future__ importprint_functionimportpytestfrom unittest importTestCasefrom selenium importwebdriverimportlogging,syslog= logging.getLogger(__name__)classTestClass(TestCase):

@classmethoddefsetUpClass(cls):

log.info('setup_class()')

cls.driver=webdriver.Firefox()

cls.driver.get("http://www.baidu.com")

log.info("xxxxxxxxxxxxxxx")

@classmethoddefteardown_class(cls):

log.info('teardown_class()')defsetUp(self):

log.info('\nsetup_method()')

self.addCleanup(self.screen_shot)defscreen_shot(self):

log.info("yyyyyyyyyyyyyy")

log.info("sereen_shot")defqqq(self):

log.info("xxxxxxxxxxxqqqq")assert 4==5

#def teardown_method(self, method):

deftearDown(self):

log.info("ffjiafuiodafdfj___teardown")

@pytest.mark.slowdeftest_7(self):importtime

time.sleep(10)

log.info('- test_7()')

@pytest.mark.qqdeftest_4(self):importpdb;pdb.set_trace()

self.result=self.addCleanup(self.qqq)

log.info('- test_4()')deftest_5(self):

log.info('- test_4()')assert 4==5

如果沒有加日志實時輸出會是怎么樣的,如下:

可以看出,日志在過程中沒有實時輸出,在實際跑項目錄,這個有點不太好看。

解決:

看看pytest是怎么解決的呢。

首先pytest是從pytest.ini中讀取log_cli配置的,默認是關(guān)閉的。如上圖中顯示,我們的pytest.ini文件是空的

再看看pytest -h文件:

關(guān)于log的help有以下:

--no-print-logs disable printing caught logs on failed tests.--log-level=LOG_LEVEL

logging level used by the logging module--log-format=LOG_FORMAT

log format as used by the logging module.--log-date-format=LOG_DATE_FORMAT

log date format as used by the logging module.--log-cli-level=LOG_CLI_LEVEL

cli logging level.--log-cli-format=LOG_CLI_FORMAT

log format as used by the logging module.--log-cli-date-format=LOG_CLI_DATE_FORMAT

log date format as used by the logging module.--log-file=LOG_FILE path to a file when logging will be written to.--log-file-level=LOG_FILE_LEVEL

log file logging level.--log-file-format=LOG_FILE_FORMAT

log format as used by the logging module.--log-file-date-format=LOG_FILE_DATE_FORMAT

log date format as used by the logging module.

View Code

然后你還會發(fā)現(xiàn)一行:

[pytest] ini-options in the first pytest.ini|tox.ini|setup.cfg file found:

所以,有兩種方法解決

1) 在當(dāng)前文件夾下寫pytest.ini或tox.ini或setup.cfg文件,然后將日志相關(guān)寫在里面,如下:

[pytest]

log_cli= 1log_cli_level=INFO

log_cli_format= %(asctime)s [%(levelname)8s] %(message)s (%(filename)s:%(lineno)s)

log_cli_date_format=%Y-%m-%d %H:%M:%S

這時就可以正常打印日志出來。

2)直接用pytest -o方式重寫,這個功能在pytest 3.4之后才實現(xiàn),如下

pytest pytest_lean2.py -o log_cli=true -o log_cli_level=INFO

結(jié)果如下:

update更新下:

實際在項目過程中,實時日志需要時間及文件名還有行號,可在后面加這樣的參數(shù):

-vv -o log_cli=true -o log_cli_level=INFO --log-date-format="%Y-%m-%d %H:%M:%S" --log-format="%(filename)s:%(lineno)s %(asctime)s %(levelname)s %(message)s"

結(jié)果就會像下面這樣

總結(jié)

以上是生活随笔為你收集整理的python实时读取日志并打印关键字怎么实现_python pytest测试框架介绍五---日志实时输出...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

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