I have a module that imports unittest and has some TestCases. I would like to accept some command line options (for example below, the name of a data file), but when I try t
I thought I'd share my solution for adding a --debug switch to the test to control the logger:
if __name__=='__main__':
parser = argparse.ArgumentParser(description="Build a compilation script")
parser.add_argument('--debug', help='Turn on debug messages', action='store_true', default=False)
args = parser.parse_args()
if args.debug:
log_level = logging.DEBUG
else:
log_level = logging.INFO
logging.basicConfig(level=log_level)
sys.argv.pop()
unittest.main()
Then I extended unittest.TestCase to add logging:
class mcs_TestCase(unittest.TestCase, object):
def __init__(self, *args, **kwargs):
super(mcs_TestCase,self).__init__(*args,**kwargs)
logging.basicConfig()
self.logger = logging.getLogger(__name__)
...
Now I can turn messages on and off in my test using --debug, but it gets ignored in regular regressions.