/[thuban]/branches/WIP-pyshapelib-bramz/test/support.py
ViewVC logotype

Annotation of /branches/WIP-pyshapelib-bramz/test/support.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 596 - (hide annotations)
Thu Apr 3 11:37:01 2003 UTC (21 years, 11 months ago) by bh
Original Path: trunk/thuban/test/support.py
File MIME type: text/x-python
File size: 5546 byte(s)
(print_garbage_information): New function that
prints information about still connected messages and memory
leaks.
(run_suite): Removed.
(run_tests): New function for use as a replacement of
unittest.main in the test_* files. This one calls
print_garbage_information at the end.

1 bh 596 # Copyright (c) 2002, 2003 by Intevation GmbH
2 bh 292 # Authors:
3     # Bernhard Herzog <[email protected]>
4     #
5     # This program is free software under the GPL (>=v2)
6     # Read the file COPYING coming with Thuban for details.
7    
8     """
9     Support classes and function for the test suite
10     """
11    
12     __version__ = "$Revision$"
13     # $Source$
14     # $Id$
15    
16     import os, sys
17     import unittest
18    
19     def thuban_dir():
20     """Return the directory containing the Thuban package"""
21     thisdir = os.path.dirname(__file__)
22     return os.path.join(thisdir, os.pardir)
23    
24    
25     def add_thuban_dir_to_path():
26     """Insert the Thuban directory at the beginning of the python path.
27    
28     If it's already part of the path, remove later occurrences.
29     """
30     dir = thuban_dir()
31     while 1:
32     try:
33     sys.path.remove(dir)
34     except ValueError:
35     break
36     sys.path.insert(0, dir)
37    
38    
39     _initthuban_done = 0
40     def initthuban():
41     """Initialize the interpreter for using Thuban modules
42     """
43     global _initthuban_done
44     if not _initthuban_done:
45     add_thuban_dir_to_path()
46     import thubaninit
47     _initthuban_done = 1
48    
49 bh 596 def run_tests():
50     """Frontend for unittest.main that prints some additional debug information
51 bh 292
52 bh 596 After calling unittest.main, run the garbage collector and print
53     uncollected objects. Also print any un-unsubscribed messages.
54     """
55     try:
56     unittest.main()
57     finally:
58     # This has to be in a finally clause because unittest.main()
59     # ends with a sys.exit to make sure that the process exits with
60     # an appropriate exit code
61     print_garbage_information()
62 bh 292
63 bh 596 def print_garbage_information():
64     """Print information about things that haven't been cleaned up.
65    
66     Run the garbage collector and print uncollected objects. Also print
67     any un-unsubscribed messages.
68     """
69     import gc, Thuban.Lib.connector
70     gc.collect()
71     if gc.garbage:
72     print
73     print "There are %d uncollected objects:" % len(gc.garbage)
74     print gc.garbage
75     Thuban.Lib.connector._the_connector.print_connections()
76    
77    
78 bh 292 def create_temp_dir():
79     """Create a temporary directory and return its name.
80    
81     The temporary directory is always called temp and is created in the
82     directory where support module is located.
83    
84     If the temp directory already exists, just return the name.
85     """
86     name = os.path.abspath(os.path.join(os.path.dirname(__file__), "temp"))
87    
88     # if the directory already exists, we're done
89     if os.path.isdir(name):
90     return name
91    
92     # create the directory
93     os.mkdir(name)
94     return name
95    
96    
97     class FileTestMixin:
98    
99     """Mixin class for tests that use files in the temporary directory
100     """
101    
102     def temp_file_name(self, basename):
103     """Return the full name of the file named basename in the temp. dir"""
104     return os.path.join(create_temp_dir(), basename)
105    
106     def temp_dir(self):
107     """Return the name of the directory for the temporary files"""
108     return create_temp_dir()
109 bh 324
110    
111     class FloatComparisonMixin:
112    
113     """
114     Mixin class for tests comparing floating point numbers.
115    
116     This class provides a few methods for testing floating point
117     operations.
118     """
119    
120     fp_epsilon = 1e-6
121    
122     def assertFloatEqual(self, test, value):
123     """Assert equality of test and value with some tolerance.
124    
125     Assert that the absolute difference between test and value is
126     less than self.fp_epsilon.
127     """
128 bh 342 self.assert_(self.fp_epsilon > abs(test - value),
129     "abs(%g - %g) >= %g" % (test, value, self.fp_epsilon))
130 bh 324
131     def assertFloatSeqEqual(self, test, value, epsilon = None):
132     """Assert equality of the sequences test and value with some tolerance.
133    
134     Assert that the absolute difference between each corresponding
135     value in test and value is less than the optional parameter
136     epsilon. If epsilon is not given use self.fp_epsilon.
137     """
138     if epsilon is None:
139     epsilon = self.fp_epsilon
140     for i in range(len(test)):
141 bh 342 self.assert_(epsilon > abs(test[i] - value[i]),
142     "abs(%g - %g) >= %g" % (test[i], value[i], epsilon))
143 bh 324
144    
145     class SubscriberMixin:
146    
147     """Mixin class for tests for messages sent through the Connector
148    
149     The SubscriberMixin has some methods that can be used as subscribers
150     of events that when called append information about the message into
151     a list of messages received.
152    
153     A derived class should call the clear_messages() method in both its
154     setUp and tearDown methods to clear the list of messages received.
155     """
156    
157     def clear_messages(self):
158     """Clear the list of received messages.
159    
160     Call this at least in the tests setUp and tearDown methods. It's
161     important to do it in tearDown too because otherwise there may
162     be cyclic references.
163     """
164     self.received_messages = []
165    
166     def subscribe_no_params(self):
167     """Method for subscriptions without parameters.
168    
169     Add an empty tuple to the list of received messages.
170     """
171     self.received_messages.append(())
172    
173     def subscribe_with_params(self, *args):
174     """Method for subscriptions with parameters.
175    
176     Append the tuple will all arguments to this function (except for
177     the self argument) to the list of received messages.
178     """
179     self.received_messages.append(args)
180    
181     def check_messages(self, messages):
182     """Check whether the messages received match the list messages"""
183     self.assertEquals(messages, self.received_messages)

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26