From 726bb4e8b86a0e147f42c07e36c0175f9e383401 Mon Sep 17 00:00:00 2001 From: James G Sack Date: Fri, 14 Dec 2007 07:34:10 +0000 Subject: [PATCH] add simple logging feature for test modules svn: r9499 --- ChangeLog | 8 ++++++ src/test/test/test_util_test.py | 22 ++++++++++++++++ src/test/test_util.py | 46 +++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+) diff --git a/ChangeLog b/ChangeLog index dba779f70..5041188c8 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +2007-12-13 Jim Sack + * src/test/test_util.py + * src/test/test/test_util_test.py + add gramps-independent (but compatible) logging to test_util + to give test modules a really simple setup call. + also added a simple logfile init and read feature + for test modules that need to look at log messages + 2007-12-12 Brian Matherly * src/PluginUtils/__init__.py: Fix MenuToolOptions. diff --git a/src/test/test/test_util_test.py b/src/test/test/test_util_test.py index b445a503c..cba7ee584 100644 --- a/src/test/test/test_util_test.py +++ b/src/test/test/test_util_test.py @@ -161,6 +161,28 @@ class Test3(U.TestCase): else: s.fail("Skip deltree constraint test, no '$HOME' var") +# logging (& misc?) +class Test4(U.TestCase): + logf = "/tmp/__tu__log__" + + def test4a(s): + wmsg = "a warning message" + emsg = "an error message" + import logging + # file logging helps with file capture of log-messages + tl = tu.TestLogger() + for i in (1,2): + # 2 passes to test clearing old file + tl.logfile_init(s.logf) + logging.warn(wmsg) + logging.info("nada") + logging.error(emsg) + ll = tl.logfile_getlines() + nl = len(ll) + s.assertEquals(nl,2, + tu.msg(nl,2, "pass %d: expected line count" % i)) + + if __name__ == "__main__": U.main() #===eof=== diff --git a/src/test/test_util.py b/src/test/test_util.py index e49431d4d..b27f08081 100644 --- a/src/test/test_util.py +++ b/src/test/test_util.py @@ -5,6 +5,8 @@ import sys import traceback import tempfile import shutil +import logging + # _caller_context is primarily here to support and document the process # of determining the test-module's directory. @@ -140,4 +142,48 @@ def delete_tree(dir): % (dir, here, tmp)) shutil.rmtree(sdir) +# simplified logging +# gramps-independent but gramps-compatible +# +# I don't see any need to inherit from logging.Logger +# (at present, test code needs nothing fancy) +# but that might be considered for future needs +# NB: current code reflects limited expertise on the +# uses of the logging module +# --------------------------------------------------------- +class TestLogger(): + """this class mainly just encapsulates some globals + namely lfname, lfh for a file log name and handle + + provides simplified logging setup for test modules + that need to setup logging for modules under test + (just instantiate a TestLogger to avoid error + messages about logging handlers not available) + + There is also a simple logfile capability, to allow + test modules to capture gramps logging output + + Note that existing logging will still occur, possibly + resulting in console messages and popup dialogs + """ + def __init__(s, lvl=logging.WARN): + logging.basicConfig(level=lvl) + + def logfile_init(s, lfname): + """init or re-init a logfile""" + if getattr(s, "lfh", None): + logging.getLogger().handlers.remove(s.lfh) + if os.path.isfile(lfname): + os.unlink(lfname) + s.lfh = logging.FileHandler(lfname) + logging.getLogger().addHandler(s.lfh) + s.lfname = lfname + + def logfile_getlines(s): + """get current content of logfile as list of lines""" + txt = [] + if s.lfname and os.path.isfile(s.lfname): + txt = open(s.lfname).readlines() + return txt + #===eof===