I have a problem with generating log files while using the test runner and I can't wrap my head around it. Basically, if I execute my test function with pytest, no log file is created. If I run the function as a script call (so normal run "test_logger", instead of run "pytest for test_logger.test_logging") the logging file is generated as expected.
As you can see in the example below it is not a complicated logger. I assume the problem has something to do with the pytest configuration but I just don't understand what is going wrong here.
I did see that Pycharm offers options for logging the console output to file etc., but I want to understand why file generation via the logging module is somehow suppressed. E.g. if I build a big e2e test to simulate correct behavior of my app I want to have everything happening like for the real thing which means in this case: generate the log where I want it and how I want it. I know that I can circumvent the issue by just running the test as a normal script but that does not help me understanding why this issue is happening in the first place.
import os
import datetime
import logging
def test_logging():
LOG_PATH = os.path.join(os.getcwd(), 'log')
if not os.path.exists(LOG_PATH):
os.mkdir(LOG_PATH)
now = datetime.datetime.now()
filename = os.path.join(LOG_PATH, 'log-'+now.strftime("%Y-%m-%d %H-%M")+'.log')
logging.basicConfig(filename=filename, level=logging.DEBUG,
format='%(asctime)s - %(levelname)s - %(message)s',
datefmt='%Y/%m/%d %H:%M:%S')
logger = logging.getLogger()
logger.debug("Test")
test_logging()