[py-svn] r13285 - py/dist/py/misc/testing

ggheo at codespeak.net ggheo at codespeak.net
Sat Jun 11 01:07:11 CEST 2005


Author: ggheo
Date: Sat Jun 11 01:07:11 2005
New Revision: 13285

Added:
   py/dist/py/misc/testing/test_log.py
Log:
Unit tests/usage examples for the log module.


Added: py/dist/py/misc/testing/test_log.py
==============================================================================
--- (empty file)
+++ py/dist/py/misc/testing/test_log.py	Sat Jun 11 01:07:11 2005
@@ -0,0 +1,86 @@
+
+import py
+import sys
+
+class TestLogging: 
+    def setup_method(self, meth): 
+        self.state = py.log._getstate_() 
+    def teardown_method(self, meth): 
+        py.log._setstate_(self.state) 
+
+    def test_log_stdout(self):
+        # We redirect stdout so that we can verify that
+        # the log messages have been printed to it
+        redirect = 'py_stdout.out'
+        sys.saved = sys.stdout
+        sys.stdout = open(redirect, 'w')
+
+        # Start of the 'consumer' code
+        py.trace[...] = py.log.stdout
+        py.trace.debug("hello world") 
+        py.trace.info("hello world") 
+        py.trace.warn("hello world") 
+        py.trace.error("hello world") 
+        py.trace.critical("hello world") 
+        # End of the 'consumer' code
+
+        sys.stdout = sys.saved
+        lines = open(redirect).readlines()
+        assert lines == ['[debug] hello world\n', '[info] hello world\n',
+                         '[warn] hello world\n', '[error] hello world\n',
+                         '[critical] hello world\n']
+
+    def test_log_stderr(self):
+        # We redirect stderr so that we can verify that
+        # the log messages have been printed to it
+        redirect = 'py_stderr.out'
+        sys.saved = sys.stderr
+        sys.stderr = open(redirect, 'w')
+
+        # Start of the 'consumer' code
+        py.trace[...] = py.log.stderr
+        py.trace.debug("hello world") 
+        py.trace.info("hello world") 
+        py.trace.warn("hello world") 
+        py.trace.error("hello world") 
+        py.trace.critical("hello world") 
+        # End of the 'consumer' code
+
+        sys.stderr = sys.saved
+        lines = open(redirect).readlines()
+        assert lines == ['[debug] hello world\n', '[info] hello world\n',
+                         '[warn] hello world\n', '[error] hello world\n',
+                         '[critical] hello world\n']
+        
+    def test_default_log_file(self):
+        # Start of the 'consumer' code
+        py.trace[...] = py.log.file
+        py.trace.debug("hello world") 
+        py.trace.info("hello world") 
+        py.trace.warn("hello world") 
+        py.trace.error("hello world") 
+        py.trace.critical("hello world")
+        # End of the 'consumer' code
+
+        lines = open('py_log.out').readlines()
+        last_5_lines = lines[-5:]
+        assert last_5_lines == ['[debug] hello world\n', '[info] hello world\n',
+                                '[warn] hello world\n', '[error] hello world\n',
+                                '[critical] hello world\n']
+
+    def test_custom_log_file(self):
+        # Start of the 'consumer' code
+        py.log['file'] = 'py_log2.out'
+        py.trace[...] = py.log.file
+        py.trace.debug("hello world") 
+        py.trace.info("hello world") 
+        py.trace.warn("hello world") 
+        py.trace.error("hello world") 
+        py.trace.critical("hello world") 
+        # End of the 'consumer' code
+
+        lines = open('py_log2.out').readlines()
+        last_5_lines = lines[-5:]
+        assert last_5_lines == ['[debug] hello world\n', '[info] hello world\n',
+                                '[warn] hello world\n', '[error] hello world\n',
+                                '[critical] hello world\n']



More information about the pytest-commit mailing list