You can change the default test runner in Project Settings/Preferences >> Tools >> Python Integrated Tools
I assume adding the -nologcapture
flag as noted in other answers probably would have worked in my case as well, but I prefer a solution that's exposed by the IDE rather than a manual override.
BTW choosing Unittest
also solves an additional error that I got once when trying to run tests - No module named 'nose'
The -s option mentioned in the question in combination with adding the StreamHandler using stream=sys.stdout did work for me.
import loggingimport syslogger = logging.getLogger('foo')logger.addHandler(logging.StreamHandler(stream=sys.stdout))logger.warning('foo')
Pycharm unit test interactive debug command line doesn't work
PyCharm suppresses the logs by adding a --no-summary flag when it runs tests by default. You can override this behaviour by going by going to File > Settings > Advanced Settings > Python and checking the option Pytest: do not add "--no-header --no-summary -q"
More detailed answer here https://stackoverflow.com/a/73873315/13466774
Add a stream handler, pointing to sys.stdout if doctest or pytest is running:
import loggingimport syslogger = logging.getLogger()def setup_doctest_logger(log_level:int=logging.DEBUG):""":param log_level::return:>>> logger.info('test') # there is no output in pycharm by default>>> setup_doctest_logger()>>> logger.info('test') # now we have the output we wanttest"""if is_pycharm_running():logger_add_streamhandler_to_sys_stdout()logger.setLevel(log_level)def is_pycharm_running()->bool:if ('docrunner.py' in sys.argv[0]) or ('pytest_runner.py' in sys.argv[0]):return Trueelse:return Falsedef logger_add_streamhandler_to_sys_stdout():stream_handler=logging.StreamHandler(stream=sys.stdout)logger.addHandler(stream_handler)