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)文章
numpy實(shí)現(xiàn)神經(jīng)網(wǎng)絡(luò)反向傳播算法的步驟
這篇文章主要介紹了numpy實(shí)現(xiàn)神經(jīng)網(wǎng)絡(luò)反向傳播算法的步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
python使用for循環(huán)和海龜繪圖實(shí)現(xiàn)漂亮螺旋線
這篇文章主要為大家介紹了python使用for循環(huán)和海龜繪圖實(shí)現(xiàn)漂亮螺旋線實(shí)現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
python 協(xié)程中的迭代器,生成器原理及應(yīng)用實(shí)例詳解
這篇文章主要介紹了python 協(xié)程中的迭代器,生成器原理及應(yīng)用,結(jié)合具體實(shí)例形式詳細(xì)分析了Python協(xié)程中的迭代器,生成器概念、原理及應(yīng)用操作技巧,需要的朋友可以參考下2019-10-10
Python使用BeautifulSoup進(jìn)行頁(yè)面解析
在Python中,我們可以使用BeautifulSoup庫(kù)來(lái)解析網(wǎng)頁(yè),BeautifulSoup提供了簡(jiǎn)單而強(qiáng)大的API,使得解析網(wǎng)頁(yè)變得輕松而高效,下面小編就來(lái)為大家詳細(xì)講講BeautifulSoup解析網(wǎng)頁(yè)的具體操作吧2023-09-09
python的import?機(jī)制是怎么實(shí)現(xiàn)的
這篇文章主要介紹了python的import?機(jī)制是怎么實(shí)現(xiàn)的,import有Python運(yùn)行時(shí)的全局模塊池的維護(hù)和搜索、解析與搜索模塊路徑的樹(shù)狀結(jié)構(gòu)等作用,下文具體相關(guān)介紹需要的小伙伴可以參考一下2022-05-05
python使用正則表達(dá)式提取網(wǎng)頁(yè)URL的方法
這篇文章主要介紹了python使用正則表達(dá)式提取網(wǎng)頁(yè)URL的方法,涉及Python中urllib模塊及正則表達(dá)式的相關(guān)使用技巧,需要的朋友可以參考下2015-05-05
Pycharm運(yùn)行加載文本出現(xiàn)錯(cuò)誤的解決方法
今天小編就為大家分享一篇Pycharm運(yùn)行加載文本出現(xiàn)錯(cuò)誤的解決方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-06-06

