less than 1 minute read

In this stackoverflow question someone asks how to view logging messages while unit testing. It got a lot of responses but none of them worked for me.

This is what worked for me to get my logs to show when testing in pycharm.

  1. Use poetry for your package management
  2. Add this to your pyproject.toml
[tool.pytest.ini_options]
log_cli = true
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"

Then the logs will show up when you hit run/debug in pycharm

Connected to pydev debugger (build 212.4746.96)
Launching pytest with arguments integration/test_preprocessing.py::TestPreprocessingFlow
============================= test session starts =============================
collecting ... collected 1 item
integration\test_preprocessing.py::TestPreprocessingFlow::test_preprocessing 
-------------------------------- live log call --------------------------------
2021-12-03 09:38:20 [    INFO] Establishing connection... (preprocess_handler.py:72)
2021-12-03 09:38:20 [    INFO] Creating session in eu-west-1 (connection.py:75)
2021-12-03 09:38:20 [    INFO] Found credentials in shared credentials file: ~/.aws/credentials (credentials.py:1225)

Hope this helps!

Subscribe

Comments