Selenium 在 pycharm 里通过 pytest 运行用例,控制台没有输出日志信息

小栋 · August 18, 2020 · Last by replied at February 26, 2021 · 4901 hits

import logging

logging.basicConfig(level=logging.DEBUG)

def test_demo():
logging.debug("This is a log!")

控制台输出:
Testing started at 17:28 ...
E:\pyDemo\seleniumDemo\venv\Scripts\python.exe "D:\pycharm\PyCharm 2018.3.5\helpers\pycharm_jb_pytest_runner.py" --path E:/pyDemo/seleniumDemo/test_log1.py
Launching pytest with arguments E:/pyDemo/seleniumDemo/test_log1.py in E:\pyDemo\seleniumDemo

============================= test session starts =============================
platform win32 -- Python 3.7.3, pytest-6.0.0, py-1.9.0, pluggy-0.13.1
rootdir: E:\pyDemo\seleniumDemocollected 1 item

test_log1.py . [100%]

============================== 1 passed in 0.15s ==============================
Process finished with exit code 0

附言 1  ·  August 18, 2020

正常不用 pytest 执行控制台就可以输出 log,好晕啊

附言 2  ·  August 18, 2020

希望各位大佬帮忙看看,自己感觉是 IDE 的问题

共收到 6 条回复 时间 点赞

Terminal 运行没有输出
直接 Run ‘pytest for/in test_log1.py’ /// ctrl + shift + F10 可以输出
至于为嘛,,没研究……

你要新建一个 pytest.ini 文件 里面写个配置 log_cli = True 就可以了

小栋 #3 · August 19, 2020 Author
nakal 回复


添加了配置后,控制台只是多了通过状态输出,是不是还少什么配置啊

小栋 #4 · August 19, 2020 Author
孓晓 回复

我这里两种方式运行都输出不了😂

pytest -s

遇到了同样的问题,查了一下午的时间终于解决了,需要在项目根目录下添加一个 pytest.ini 文件,并在文件中输入如下内容:
[pytest]
log_cli = 1
log_cli_level = INFO
log_cli_format = %(asctime) s [%(levelname) 8s] %(message) s (%(filename) s:%(lineno) s)
log_cli_date_format=%Y-%m-%d %H:%M:%S
具体见如下链接,作者还提供了其他方法,可参考。
https://www.cnblogs.com/landhu/p/9910460.html

需要 Sign In 后方可回复, 如果你还没有账号请点击这里 Sign Up