Pytes正確的配置使用日志功能
在pytest自動化測試中,如果只是簡單的從應(yīng)用的角度來說,完全可以不去了解pytest中的顯示信息的部分以及原理,完全可以通過使用推薦的pytest.ini配置,從而可以做到相對來說比較通用的日志配置。
這里我們推薦使用如下配置,其中l(wèi)og_cli 相關(guān)的四條配置是用來配置live log即實時日志的,而其他三條配置則是用例配置capture log即捕獲日志的。分別對他們的日志級別、日志格式、時間戳格式進行了設(shè)置,比如這里日志級別都設(shè)置為info,當(dāng)然如果腳本穩(wěn)定之后,提交自動化測試代碼倉庫時可以將日志級別調(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
下面用一個簡單的測試腳本來展示一下上述日志配置的效果。
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é)果如下,可以看出這里顯示了實時日志(live log),同時未顯示debug級別的日志,捕獲日志(capture log)同樣也未顯示debug級別的日志,而且時間戳和日志格式相對來說都是比較符合實際應(yīng)用的,因此這里的推薦的pytest.ini中對日志的配置,完全可以拿去直接使用。
(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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Pearson相關(guān)系數(shù)和Spearman相關(guān)系數(shù)的區(qū)別及說明
這篇文章主要介紹了Pearson相關(guān)系數(shù)和Spearman相關(guān)系數(shù)的區(qū)別及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-05-05PyTorch 如何將CIFAR100數(shù)據(jù)按類標(biāo)歸類保存
這篇文章主要介紹了PyTorch 將CIFAR100數(shù)據(jù)按類標(biāo)歸類保存的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-05-05Numpy中np.random.rand()和np.random.randn() 用法和區(qū)別詳解
這篇文章主要介紹了Numpy中np.random.rand()和np.random.randn() 用法和區(qū)別詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10Tensorflow分類器項目自定義數(shù)據(jù)讀入的實現(xiàn)
這篇文章主要介紹了Tensorflow分類器項目自定義數(shù)據(jù)讀入的實現(xiàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-02-02