I'm working on a project that creates a different log file each time it runs. I have some unit tests that test my code but in the process also cause the log files to get created. My application code looks like this:
module.py
import logging
from uuid import uuid4
class TestRun:
def __init__(self):
self.test_run_id = str(uuid4())
self.logger = logging.getLogger(__name__)
self.logger.setLevel(logging.INFO)
handler = logging.FileHandler('{}.log'.format(self.test_run_id))
self.logger.addHandler(handler)
def prep_test_run(self):
self.logger.info('Starting prep')
if __name__ == '__main__':
tr = TestRun()
tr.prep_test_run()
My test code is something like this:
import unittest
from module import TestRun
class TestModule(unittest.TestCase):
def test_function(self):
tr = TestRun()
tr.prep_test_run()
Every time I run the unit tests files get created. Is there a way to disable this while running the unit tests. I've tried to set the log level in def setUp(self) but it didn't work.
test=1and in those cases, you create if statements to skip the steps in the code which make the log files you don't want.setUp, Unit Tests support atearDown()method to handle things exactly like this.