为什么我的打印语句和logging.info没有显示在pytest html报告中

问题描述 投票:0回答:2

我在 pytest 框架中运行测试用例时发现了这个问题,一旦测试完成(通过或失败),在 report.html 上,当我单击“显示每个测试用例的详细信息”时,我会得到

"No Log Output Captured"
报告.html

我用来获取 html 报告的命令:

pytest test_01.py -s -v --html=report.html

在控制台中我可以获得打印语句。但是有没有什么方法可以让我在report.html中也得到同样的结果?

控制台输出:

collected 1 item                                                                                                                                                      
------------------------ Test 1 Initiated: Verify LogRetention file count in dropbox ------------------------
Device rebooting...
------------------------ Test 1 PASSED successfully : Verify LogRetention file count in dropbox folder ------------------------


Test case took 118.19 seconds.

 test_01.py::test_verify_file_count ✓  
python pytest pytest-html
2个回答
0
投票

你必须使用记录器而不是打印

import logging

def test_example():
    # Set up the logger
    logger = logging.getLogger(__name__)

    # Example log messages
    logger.debug("This is a debug message")
    logger.info("This is an info message")
    logger.warning("This is a warning message")
    logger.error("This is an error message")
    logger.critical("This is a critical message")

您还可以使用

--capture=tee-sys
查看标准输出和日志输出

pytest --capture=tee-sys

希望对你有帮助


0
投票

简单。在您在终端中执行的命令中添加“--capture=tee-sys”。

即。

pytest -v -s file.py --html=report.html --capture=tee-sys

© www.soinside.com 2019 - 2024. All rights reserved.