Pytest----如何正确的配置使用日志功能
作者:redrose2100   类别:    日期:2022-12-05 16:46:28    阅读:1224 次   消耗积分:0 分

    在pytest自动化测试中,如果只是简单的从应用的角度来说,完全可以不去了解pytest中的显示信息的部分以及原理,完全可以通过使用推荐的pytest.ini配置,从而可以做到相对来说比较通用的日志配置。
    这里我们推荐使用如下配置,其中log_cli 相关的四条配置是用来配置live log即实时日志的,而其他三条配置则是用例配置capture log即捕获日志的。分别对他们的日志级别、日志格式、时间戳格式进行了设置,比如这里日志级别都设置为info,当然如果脚本稳定之后,提交自动化测试代码仓库时可以将日志级别调整为warning。

    1. [pytest]
    2. log_cli = True
    3. log_cli_level = info
    4. log_cli_format = %(asctime)s | %(levelname)s | %(filename)s:%(lineno)s | %(message)s"
    5. log_cli_date_format = %Y-%m-%d %H:%M:%S
    6. log_level = info
    7. log_format = %(asctime)s | %(levelname)s | %(filename)s:%(lineno)s | %(message)s"
    8. log_date_format = %Y-%m-%d %H:%M:%S

    下面用一个简单的测试脚本来展示一下上述日志配置的效果。

    1. import logging
    2. def test_demo():
    3. logging.debug("this is debug log ...")
    4. logging.info("this is info log ...")
    5. logging.warning("this is warning log ...")
    6. logging.error("this is error log ...")
    7. logging.critical("this is critical log ...")
    8. assert 1==2

    执行结果如下,可以看出这里显示了实时日志(live log),同时未显示debug级别的日志,捕获日志(capture log)同样也未显示debug级别的日志,而且时间戳和日志格式相对来说都是比较符合实际应用的,因此这里的推荐的pytest.ini中对日志的配置,完全可以拿去直接使用。

    1. (demo-HCIhX0Hq) E:\demo>pytest
    2. =================== test session starts ===================
    3. platform win32 -- Python 3.7.9, pytest-7.2.0, pluggy-1.0.0
    4. rootdir: E:\demo, configfile: pytest.ini
    5. plugins: assume-2.4.3, rerunfailures-10.2
    6. collected 1 item
    7. test_demo.py::test_demo
    8. ---------------------- live log call ----------------------
    9. 2022-12-06 00:42:06 | INFO | test_demo.py:5 | this is info log ..."
    10. 2022-12-06 00:42:06 | WARNING | test_demo.py:6 | this is warning log ..."
    11. 2022-12-06 00:42:06 | ERROR | test_demo.py:7 | this is error log ..."
    12. 2022-12-06 00:42:06 | CRITICAL | test_demo.py:8 | this is critical log ..."
    13. FAILED [100%]
    14. ======================== FAILURES =========================
    15. ________________________ test_demo ________________________
    16. def test_demo():
    17. logging.debug("this is debug log ...")
    18. logging.info("this is info log ...")
    19. logging.warning("this is warning log ...")
    20. logging.error("this is error log ...")
    21. logging.critical("this is critical log ...")
    22. > assert 1==2
    23. E assert 1 == 2
    24. test_demo.py:9: AssertionError
    25. -------------------- Captured log call --------------------
    26. 2022-12-06 00:42:06 | INFO | test_demo.py:5 | this is info log ..."
    27. 2022-12-06 00:42:06 | WARNING | test_demo.py:6 | this is warning log ..."
    28. 2022-12-06 00:42:06 | ERROR | test_demo.py:7 | this is error log ..."
    29. 2022-12-06 00:42:06 | CRITICAL | test_demo.py:8 | this is critical log ..."
    30. ================= short test summary info =================
    31. FAILED test_demo.py::test_demo - assert 1 == 2
    32. ==================== 1 failed in 0.07s ====================
    33. (demo-HCIhX0Hq) E:\demo>
    始终坚持开源开放共享精神,同时感谢您的充电鼓励和支持!
    版权所有,转载本站文章请注明出处:redrose2100, http://blog.redrose2100.com/article/482
    个人成就
    • 博客总数: 613 
    • 阅读总量: 712484 
    • 2022年 : 371 篇 
    • 2023年 : 211 篇 
    • 2024年 : 31 篇 
    • 2025年 : 0 篇 
    测试开发技术全栈公众号
    DevOps技术交流微信群