add simple logging feature for test modules
svn: r9499
This commit is contained in:
parent
6bf7737025
commit
726bb4e8b8
@ -1,3 +1,11 @@
|
||||
2007-12-13 Jim Sack <jgsack@san.rr.com>
|
||||
* 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 <brian@gramps-project.org>
|
||||
* src/PluginUtils/__init__.py: Fix MenuToolOptions.
|
||||
|
||||
|
@ -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===
|
||||
|
@ -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===
|
||||
|
Loading…
Reference in New Issue
Block a user