[py-svn] r57565 - in py/trunk/py/test: . testing

hpk at codespeak.net hpk at codespeak.net
Thu Aug 21 19:39:35 CEST 2008


Author: hpk
Date: Thu Aug 21 19:39:34 2008
New Revision: 57565

Modified:
   py/trunk/py/test/config.py
   py/trunk/py/test/testing/test_config.py
Log:
also introduce config.maketrace(name, flush=False) which 
returns either a nulltracer or opens a log in the tracedir
and returns an object that you can call with args to print
into the file. 



Modified: py/trunk/py/test/config.py
==============================================================================
--- py/trunk/py/test/config.py	(original)
+++ py/trunk/py/test/config.py	Thu Aug 21 19:39:34 2008
@@ -226,6 +226,33 @@
         if self.option.tracedir is not None:
             return py.path.local(self.option.tracedir)
 
+    def maketrace(self, name, flush=False):
+        """ return a tracedirectory or None, depending on --tracedir. """
+        tracedir = self.gettracedir()
+        if tracedir is None:
+            return NullTracer()
+        return Tracer(tracedir.join(name), flush=flush)
+
+class Tracer(object):
+    file = None
+    def __init__(self, path, flush=False):
+        self.file = path.open(mode='w')
+        self.flush = flush
+       
+    def __call__(self, *args):
+        print >>self.file, " ".join(map(str, args))
+        if self.flush:
+            self.file.flush()
+
+    def close(self):
+        self.file.close()
+
+class NullTracer:
+    def __call__(self, *args):
+        pass
+    def close(self):
+        pass
+    
 # this is the one per-process instance of py.test configuration 
 config_per_process = Config()
 

Modified: py/trunk/py/test/testing/test_config.py
==============================================================================
--- py/trunk/py/test/testing/test_config.py	(original)
+++ py/trunk/py/test/testing/test_config.py	Thu Aug 21 19:39:34 2008
@@ -201,13 +201,28 @@
         s = eventlog.read()
         assert s.find("TestrunStart") != -1
 
-    def test_tracedir(self):
+    def test_tracedir_tracer(self):
         tracedir = self.tmpdir.mkdir("tracedir")
         config = py.test.config._reparse([self.tmpdir, 
                                           '--tracedir=%s' % tracedir])
         assert config.gettracedir() == tracedir
+
+        trace = config.maketrace("trace1.log", flush=True)
+        trace("hello", "world")
+        class A: pass 
+        trace(A())
+        p = tracedir.join("trace1.log")
+        lines = p.readlines(cr=0)
+        assert lines[0] == "hello world"
+        assert lines[1].find("A") != -1
+        trace.close()
+
+    def test_trace_null(self):
         config = py.test.config._reparse([self.tmpdir])
         assert config.gettracedir() is None
+        trace = config.maketrace("hello", flush=True)
+        trace("hello", "world")
+        trace.close()
 
     def test_implied_dsession(self):
         for x in 'startserver runbrowser rest'.split():



More information about the pytest-commit mailing list