Pytes正確的配置使用日志功能
在pytest自動(dòng)化測(cè)試中,如果只是簡(jiǎn)單的從應(yīng)用的角度來(lái)說(shuō),完全可以不去了解pytest中的顯示信息的部分以及原理,完全可以通過(guò)使用推薦的pytest.ini配置,從而可以做到相對(duì)來(lái)說(shuō)比較通用的日志配置。
這里我們推薦使用如下配置,其中l(wèi)og_cli 相關(guān)的四條配置是用來(lái)配置live log即實(shí)時(shí)日志的,而其他三條配置則是用例配置capture log即捕獲日志的。分別對(duì)他們的日志級(jí)別、日志格式、時(shí)間戳格式進(jìn)行了設(shè)置,比如這里日志級(jí)別都設(shè)置為info,當(dāng)然如果腳本穩(wěn)定之后,提交自動(dòng)化測(cè)試代碼倉(cāng)庫(kù)時(shí)可以將日志級(jí)別調(diào)整為warning。
[pytest] log_cli = True log_cli_level = info log_cli_format = %(asctime)s | %(levelname)s | %(filename)s:%(lineno)s | %(message)s" log_cli_date_format = %Y-%m-%d %H:%M:%S log_level = info log_format = %(asctime)s | %(levelname)s | %(filename)s:%(lineno)s | %(message)s" log_date_format = %Y-%m-%d %H:%M:%S
下面用一個(gè)簡(jiǎn)單的測(cè)試腳本來(lái)展示一下上述日志配置的效果。
import logging def test_demo(): logging.debug("this is debug log ...") logging.info("this is info log ...") logging.warning("this is warning log ...") logging.error("this is error log ...") logging.critical("this is critical log ...") assert 1==2
執(zhí)行結(jié)果如下,可以看出這里顯示了實(shí)時(shí)日志(live log),同時(shí)未顯示debug級(jí)別的日志,捕獲日志(capture log)同樣也未顯示debug級(jí)別的日志,而且時(shí)間戳和日志格式相對(duì)來(lái)說(shuō)都是比較符合實(shí)際應(yīng)用的,因此這里的推薦的pytest.ini中對(duì)日志的配置,完全可以拿去直接使用。
(demo-HCIhX0Hq) E:\demo>pytest =================== test session starts =================== platform win32 -- Python 3.7.9, pytest-7.2.0, pluggy-1.0.0 rootdir: E:\demo, configfile: pytest.ini plugins: assume-2.4.3, rerunfailures-10.2 collected 1 item test_demo.py::test_demo ---------------------- live log call ---------------------- 2022-12-06 00:42:06 | INFO | test_demo.py:5 | this is info log ..." 2022-12-06 00:42:06 | WARNING | test_demo.py:6 | this is warning log ..." 2022-12-06 00:42:06 | ERROR | test_demo.py:7 | this is error log ..." 2022-12-06 00:42:06 | CRITICAL | test_demo.py:8 | this is critical log ..." FAILED [100%] ======================== FAILURES ========================= ________________________ test_demo ________________________ def test_demo(): logging.debug("this is debug log ...") logging.info("this is info log ...") logging.warning("this is warning log ...") logging.error("this is error log ...") logging.critical("this is critical log ...") > assert 1==2 E assert 1 == 2 test_demo.py:9: AssertionError -------------------- Captured log call -------------------- 2022-12-06 00:42:06 | INFO | test_demo.py:5 | this is info log ..." 2022-12-06 00:42:06 | WARNING | test_demo.py:6 | this is warning log ..." 2022-12-06 00:42:06 | ERROR | test_demo.py:7 | this is error log ..." 2022-12-06 00:42:06 | CRITICAL | test_demo.py:8 | this is critical log ..." ================= short test summary info ================= FAILED test_demo.py::test_demo - assert 1 == 2 ==================== 1 failed in 0.07s ==================== (demo-HCIhX0Hq) E:\demo>
到此這篇關(guān)于Pytes正確的配置使用日志功能的文章就介紹到這了,更多相關(guān)Pytes日志配置內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python實(shí)現(xiàn)Logger打印功能的方法詳解
最近工作中遇到了打印的需求,通過(guò)查找相關(guān)的資料發(fā)現(xiàn)Python中Logger可以很好的實(shí)現(xiàn)打印,所以下面這篇文章主要給大家介紹了關(guān)于Python如何實(shí)現(xiàn)Logger打印功能的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下。2017-09-09python爬蟲(chóng)智能翻頁(yè)批量下載文件的實(shí)例詳解
在本篇文章里小編給大家整理的是一篇關(guān)于python爬蟲(chóng)智能翻頁(yè)批量下載文件的實(shí)例詳解內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。2021-02-02簡(jiǎn)單實(shí)現(xiàn)python聊天程序
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)簡(jiǎn)單聊天程序的相關(guān)代碼,包括客戶端和服務(wù)端,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04Pearson相關(guān)系數(shù)和Spearman相關(guān)系數(shù)的區(qū)別及說(shuō)明
這篇文章主要介紹了Pearson相關(guān)系數(shù)和Spearman相關(guān)系數(shù)的區(qū)別及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05PyTorch 如何將CIFAR100數(shù)據(jù)按類標(biāo)歸類保存
這篇文章主要介紹了PyTorch 將CIFAR100數(shù)據(jù)按類標(biāo)歸類保存的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-05-05Numpy中np.random.rand()和np.random.randn() 用法和區(qū)別詳解
這篇文章主要介紹了Numpy中np.random.rand()和np.random.randn() 用法和區(qū)別詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10Tensorflow分類器項(xiàng)目自定義數(shù)據(jù)讀入的實(shí)現(xiàn)
這篇文章主要介紹了Tensorflow分類器項(xiàng)目自定義數(shù)據(jù)讀入的實(shí)現(xiàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-02-02