Contact: fumanchu@aminus.org

Log in as guest/dejavu to create tickets

I think I've seen this ORM somewhere before...

root/branches/crazycache/dejavu/test/test.py

Revision 546 (checked in by fumanchu, 4 years ago)

Added --conquer option to test.py.

  • Property svn:eol-style set to native
Line 
1 """A harness for running a suite of Dejavu tests all at once.
2
3 Should be invoked as a script.
4 """
5
6 import getopt
7 import os
8 localDir = os.path.dirname(__file__)
9 import sys
10 import unittest
11
12
13 class djvTestHarness(object):
14     """A test harness for Dejavu."""
15    
16     cover = False
17     olap = False
18     mediated = False
19     conquer = False
20    
21     def __init__(self, available_tests):
22         """Constructor to populate the TestHarness instance.
23         
24         available_tests should be a list of module or store names.
25         """
26         self.available_tests = available_tests
27         self.tests = []
28    
29     def load(self, args=sys.argv[1:]):
30         """Populate a TestHarness from sys.argv.
31         
32         args defaults to sys.argv[1:], but you can provide a different
33             set of args if you like.
34         """
35        
36         longopts = ['help', 'cover', 'olap', 'mediated', 'conquer']
37         longopts.extend(self.available_tests)
38         try:
39             opts, args = getopt.getopt(args, "", longopts)
40         except getopt.GetoptError:
41             # print help information and exit
42             self.help()
43             sys.exit(2)
44        
45         self.tests = []
46        
47         for o, a in opts:
48             if o == '--help':
49                 self.help()
50                 sys.exit()
51             elif o == '--cover':
52                 self.cover = True
53             elif o == '--olap':
54                 self.olap = True
55             elif o == '--mediated':
56                 self.mediated = True
57             elif o == '--conquer':
58                 self.conquer = True
59             else:
60                 o = o[2:]
61                 if o and o not in self.tests:
62                     self.tests.append(o)
63        
64         if not self.tests:
65             self.tests = self.available_tests[:]
66    
67     def help(self):
68         """Print help for test.py command-line options."""
69        
70         print """
71 Dejavu Test Program
72     Usage:
73         test.py --help --olap --cover --mediated --conquer --<testname>
74         
75         --help: This help screen
76         --olap: Run the OLAP fixture instead of the default OLTP fixture
77         --cover: Run coverage tools
78         --mediated: Use a VerticalPartitioner to mediate the store(s).
79         --conquer: use pyconquer to trace calls.
80         
81         tests:"""
82         for name in self.available_tests:
83             print '        --' + name
84    
85     def run(self, conf=None):
86         """Run the test harness."""
87         self.load()
88        
89         if self.cover:
90             self.start_coverage()
91             try:
92                 self._run()
93             finally:
94                 self.stop_coverage()
95         elif self.conquer:
96             import pyconquer
97             f = os.path.join(os.path.dirname(__file__), "conquer.log")
98             tr = pyconquer.Logger("dejavu", events=pyconquer.all_events)
99             tr.out = open(f, "wb")
100             try:
101                 tr.start()
102                 self._run()
103             finally:
104                 tr.stop()
105                 tr.out.close()
106         else:
107             self._run()
108    
109     def _run(self):
110         # Delay the import of any dejavu module so coverage can work
111         # on the import (including class and func defs) as well.
112         from dejavu.test import tools
113         tools.prefer_parent_path()
114        
115         import dejavu
116         print "Python version:", sys.version.split()[0]
117         print "Dejavu version:", dejavu.__version__
118        
119         for name in self.tests:
120             print
121             if name.startswith("test_"):
122                 print "Running test module %r" % name
123                 suite = unittest.TestLoader().loadTestsFromName(name)
124                 from dejavu.test import tools
125                 tools.djvTestRunner.run(suite)
126             else:
127                 from dejavu.test import stores
128                 stores.run(name, self.olap, self.mediated)
129    
130     def start_coverage(self):
131         """Start the coverage tool.
132         
133         To use this feature, you need to download 'coverage.py',
134         either Gareth Rees' original implementation:
135         http://www.garethrees.org/2001/12/04/python-coverage/
136         
137         or Ned Batchelder's enhanced version:
138         http://www.nedbatchelder.com/code/modules/coverage.html
139         
140         If neither module is found in PYTHONPATH,
141         coverage is silently(!) disabled.
142         """
143         try:
144             from coverage import the_coverage as coverage
145             c = os.path.join(localDir, "coverage.cache")
146             coverage.cache_default = c
147             if c and os.path.exists(c):
148                 os.remove(c)
149             coverage.start()
150         except ImportError:
151             coverage = None
152         self.coverage = coverage
153    
154     def stop_coverage(self):
155         """Stop the coverage tool, save results, and report."""
156         if self.coverage:
157             self.coverage.save()
158             self.report_coverage()
159    
160     def report_coverage(self):
161         """Print a summary from the code coverage tool."""
162        
163         # Assume we want to cover everything in "../../dejavu/"
164         basedir = os.path.normpath(os.path.join(os.getcwd(), localDir, '../'))
165         basedir = basedir.lower()
166         self.coverage.get_ready()
167        
168         morfs = []
169         for x in self.coverage.cexecuted:
170             if x.lower().startswith(basedir):
171                 morfs.append(x)
172        
173         total_statements = 0
174         total_executed = 0
175        
176         print
177         print "CODE COVERAGE (this might take a while)",
178         for morf in morfs:
179             sys.stdout.write(".")
180             sys.stdout.flush()
181 ##            name = os.path.split(morf)[1]
182             if morf.find('test') != -1:
183                 continue
184             try:
185                 _, statements, _, missing, readable  = self.coverage.analysis2(morf)
186                 n = len(statements)
187                 m = n - len(missing)
188                 total_statements = total_statements + n
189                 total_executed = total_executed + m
190             except KeyboardInterrupt:
191                 raise
192             except:
193                 # No, really! We truly want to ignore any other errors.
194                 pass
195        
196         pc = 100.0
197         if total_statements > 0:
198             pc = 100.0 * total_executed / total_statements
199        
200         print ("\nTotal: %s Covered: %s Percent: %2d%%"
201                % (total_statements, total_executed, pc))
202
203
204 def run():
205    
206     avail = ['test_analysis',
207              'test_containers',
208              'test_dejavu',
209              
210              'ram',
211              'shelve',
212              'proxy',
213              'caching',
214              'aged',
215              'burned',
216              'fs',
217            
218              'firebird',
219              'json',
220              'memcached',
221              'memcached2',
222              'msaccess',
223              'mysql',
224              'psycopg',
225              'pypgsql',
226              'sqlite',
227              'sqlserver',
228              ]
229     djvTestHarness(avail).run()
230
231
232 if __name__ == '__main__':
233     run()
Note: See TracBrowser for help on using the browser.