source: anuga_core/source/anuga/utilities/log_test.py @ 6902

Last change on this file since 6902 was 6902, checked in by rwilson, 15 years ago

Back-merge from Numeric trunk to numpy branch.

File size: 3.3 KB
Line 
1#!/usr/bin/env python
2
3import os
4import sys
5import unittest
6import logging
7import log
8
9LOGFILE_NAME = 'test.log'
10STDOUT_LOG_NAME = 'stdout.log'
11
12
13class Test_Log(unittest.TestCase):
14    def setUp(self):
15        # just in case
16        if os.path.exists(LOGFILE_NAME):
17            os.remove(LOGFILE_NAME)
18        if os.path.exists(STDOUT_LOG_NAME):
19            os.remove(STDOUT_LOG_NAME)
20
21        # set log module logfile name
22        log.log_filename = LOGFILE_NAME
23
24        # set logging levels for this test
25        log.console_logging_level = logging.INFO
26        log.log_logging_level = logging.DEBUG
27
28    def tearDown(self):
29        if os.path.exists(LOGFILE_NAME):
30            os.remove(LOGFILE_NAME)
31        if os.path.exists(STDOUT_LOG_NAME):
32            os.remove(STDOUT_LOG_NAME)
33
34    ##
35    # @brief Test the logging routines.
36    # @note HAVE ONE TEST CASE ONLY! Multiple tests would concatenate
37    #       multiple test output in one log file.
38    def test_simple(self):
39        '''Check that logging works in simple case.'''
40
41        # vvvvv  WARNING - DO NOT REFORMAT THESE LINES!  vvvvv
42        log_expect = '''2009-03-23 12:16:53,487 CRITICAL                       log:0   |Logfile is 'test.log' with logging level of DEBUG, console logging level is INFO
432009-03-23 12:16:53,488 DEBUG                     test_log:37  |test at level DEBUG
442009-03-23 12:35:26,107 INFO                      test_log:97  |Resource usage: memory=0.0 resident=0.0 stacksize=0.0
452009-03-23 12:16:53,488 INFO                      test_log:38  |test at level INFO'''
46
47        stdout_expect = '''Logfile is 'test.log' with logging level of DEBUG, console logging level is INFO
48Resource usage: memory=0.0 resident=0.0 stacksize=0.0
49test at level INFO'''
50        # ^^^^^  WARNING - DO NOT REFORMAT THESE LINES!  ^^^^^
51
52        # capture stdout to a file
53        save_stdout = sys.stdout
54        save_stderr = sys.stderr
55        sys.stdout = sys.stderr = open(STDOUT_LOG_NAME, 'w')
56
57        # do some logging
58        log.debug('test at level DEBUG')
59        log.resource_usage(logging.INFO)
60        log.info('test at level INFO')
61
62        # put stdout/stderr back to normal
63        sys.stderr = save_stderr
64        sys.stdout = save_stdout
65
66        # check logfile is as expected
67        fd = open(LOGFILE_NAME, 'r')
68        lines = fd.readlines()
69        fd.close()
70        result = []
71        for line in lines:
72            l = line.strip('\n')
73            ndx = l.index('|')
74            result.append([l.split()[2], l[ndx:]])
75        expected = []
76        for line in log_expect.split('\n'):
77            ndx = line.index('|')
78            expected.append([line.split()[2], line[ndx:]])
79        self.failUnlessEqual(result, expected)
80
81        # check that captured stdout is as expected
82        fd = open(STDOUT_LOG_NAME, 'r')
83        lines = fd.readlines()
84        fd.close()
85        result = []
86        for line in lines:
87            l = line.strip('\n')
88            result.append(l)
89        expected = []
90        for line in stdout_expect.split('\n'):
91            expected.append(line)
92        self.failUnlessEqual(result, expected)
93
94################################################################################
95
96if __name__ == "__main__":
97    suite = unittest.makeSuite(Test_Log, 'test')
98    runner = unittest.TextTestRunner()
99    runner.run(suite)
Note: See TracBrowser for help on using the repository browser.