Search code examples
pythonunit-testingloggingpytest

Pytest Logging messages appear twice


My testing framework has below structure

  • Master_test_Class.py ---> Holds generic test cases to be run for smoke and regression test suite
  • Test_Smoke1.py and Test_Reg1.py --> Child classes inherit Master_test_class.py

enter image description here

I have logging enabled in pytest.ini for INFO

[pytest]
log_cli = 1 
log_cli_level = INFO

Below is my code in conftest.py

def pytest_generate_tests(metafunc):
.....
logging.info("This is generated during the test collection !!!")

When i run the test either of the test files, logs are printed 2 times once in the formatting specified in pytest.ini and another in red color

pytest -s Test_Reg1.py

enter image description here

I am so lost why is the logging info getting printed twice.


Solution

  • If you want to see only the output of logging module, use --log-cli-level=INFO as an argument to pytest test run. You are seeing it twice because of -s switch. And, to test whether it's the same log or different, add timestamp to the log message.