網站首頁 編程語言 正文
在pytest自動化測試中,如果只是簡單的從應用的角度來說,完全可以不去了解pytest中的顯示信息的部分以及原理,完全可以通過使用推薦的pytest.ini配置,從而可以做到相對來說比較通用的日志配置。
這里我們推薦使用如下配置,其中log_cli 相關的四條配置是用來配置live log即實時日志的,而其他三條配置則是用例配置capture log即捕獲日志的。分別對他們的日志級別、日志格式、時間戳格式進行了設置,比如這里日志級別都設置為info,當然如果腳本穩定之后,提交自動化測試代碼倉庫時可以將日志級別調整為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
執行結果如下,可以看出這里顯示了實時日志(live log),同時未顯示debug級別的日志,捕獲日志(capture log)同樣也未顯示debug級別的日志,而且時間戳和日志格式相對來說都是比較符合實際應用的,因此這里的推薦的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>
原文鏈接:https://blog.csdn.net/redrose2100/article/details/128195838
相關推薦
- 2023-01-02 Kotlin?fun函數使用方法_Android
- 2022-09-06 golang實現命令行程序的使用幫助功能_Golang
- 2022-03-14 事務隔離級別 在spring實戰
- 2022-04-25 C#實現簡單串口通信_C#教程
- 2022-07-18 SQL?Server使用T-SQL進階之公用表表達式(CTE)_MsSql
- 2022-06-19 docker容器非root用戶提權的問題解決_docker
- 2022-09-22 springboot整合log4j2報錯Unexpected filename extension
- 2022-10-15 QT?UDP網絡編程實現簡單消息傳輸_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支