Always enable logging for unittests
authorMichael Hanselmann <hansmi@google.com>
Wed, 15 Sep 2010 18:23:01 +0000 (20:23 +0200)
committerMichael Hanselmann <hansmi@google.com>
Thu, 16 Sep 2010 10:26:28 +0000 (12:26 +0200)
By enabling all log levels, we ensure all calls are fully evaluated.
There was one case in the workerpool where a call to “logging.debug”
was wrong, but not caught in unittests because debug logging was
disabled.

The optional environment variable “LOGTOSTDERR” can be set to
write all log messages to stderr instead of /dev/null.

Signed-off-by: Michael Hanselmann <hansmi@google.com>
Reviewed-by: Iustin Pop <iustin@google.com>

test/testutils.py

index fd4655a..3c46a0c 100644 (file)
@@ -35,12 +35,32 @@ def GetSourceDir():
   return os.environ.get("TOP_SRCDIR", ".")
 
 
+def _SetupLogging(verbose):
+  """Setupup logging infrastructure.
+
+  """
+  fmt = logging.Formatter("%(asctime)s: %(threadName)s"
+                          " %(levelname)s %(message)s")
+
+  if verbose:
+    handler = logging.StreamHandler()
+  else:
+    handler = logging.FileHandler(os.devnull, "a")
+
+  handler.setLevel(logging.NOTSET)
+  handler.setFormatter(fmt)
+
+  root_logger = logging.getLogger("")
+  root_logger.setLevel(logging.NOTSET)
+  root_logger.addHandler(handler)
+
+
 class GanetiTestProgram(unittest.TestProgram):
   def runTests(self):
-    """
+    """Runs all tests.
 
     """
-    logging.basicConfig(filename=os.devnull)
+    _SetupLogging("LOGTOSTDERR" in os.environ)
 
     sys.stderr.write("Running %s\n" % self.progName)
     sys.stderr.flush()