Why my print statements and logging.info is not shown in pytest html report

469 Views Asked by At

I found this issue while running a test case in pytest framework in which once the test completes (either pass or fails) On report.html I am getting "No Log Output Captured" when i click on show details on each test cases. report.html

report.html command I use to get html report: pytest test_01.py -s -v --html=report.html

In console I am able to get the print statement. however is there any possible ways that I can get the same in report.html as well?

console output:

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 ✓  
2

There are 2 best solutions below

6
Eugenio On

you have to use logger instead of print

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")

You can also use --capture=tee-sys to see both stdout and log output

pytest --capture=tee-sys

I hope it helps

0
user23419241 On

Simple. Add "--capture=tee-sys" in the command that you are executing in the terminnal.

ie.

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