Karun
Karun

Reputation: 621

pytest option to output print/logging statements from tested code

Upvotes: 7

Views: 9908

Answers (2)

Marc
Marc

Reputation: 1629

Pytest also has it's own implementation to show you whats happening as it captures your logging records. If you go to Live Logs on this page: https://docs.pytest.org/en/latest/logging.html it is explained pretty well. You need a pytest.ini file where you set:

[pytest]
log_cli = True

This will make your logs show on the terminal as they are emitted. Then you can set the level that you want to see with your pytest call to for example DEBUG:

pytest --log-cli-level DEBUG

or you can specify it in your pytest.ini as well:

[pytest]
log_cli = True
log_cli_level = DEBUG

Where log_cli_level sets the level of what logs are shown. This approach does not make you change your own code which is nice. This off course also requires you to use logging in the first place but that's a good habit to have anyway.

Upvotes: 8

Karun
Karun

Reputation: 621

There is an excellent article on this subject: https://pythontesting.net/framework/pytest/pytest-logging-real-time/

  • I converted all my print statements to logging statements in my underlying code
  • To the module containing my test_transform() function I added

`

import logging
logging.basicConfig(level=logging.DEBUG)

def test_transform()
   ...

Upvotes: 1

Related Questions