1 | n/a | #!/usr/bin/env python3 |
---|
2 | n/a | """ |
---|
3 | n/a | GUI framework and application for use with Python unit testing framework. |
---|
4 | n/a | Execute tests written using the framework provided by the 'unittest' module. |
---|
5 | n/a | |
---|
6 | n/a | Updated for unittest test discovery by Mark Roddy and Python 3 |
---|
7 | n/a | support by Brian Curtin. |
---|
8 | n/a | |
---|
9 | n/a | Based on the original by Steve Purcell, from: |
---|
10 | n/a | |
---|
11 | n/a | http://pyunit.sourceforge.net/ |
---|
12 | n/a | |
---|
13 | n/a | Copyright (c) 1999, 2000, 2001 Steve Purcell |
---|
14 | n/a | This module is free software, and you may redistribute it and/or modify |
---|
15 | n/a | it under the same terms as Python itself, so long as this copyright message |
---|
16 | n/a | and disclaimer are retained in their original form. |
---|
17 | n/a | |
---|
18 | n/a | IN NO EVENT SHALL THE AUTHOR BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, |
---|
19 | n/a | SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF |
---|
20 | n/a | THIS CODE, EVEN IF THE AUTHOR HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH |
---|
21 | n/a | DAMAGE. |
---|
22 | n/a | |
---|
23 | n/a | THE AUTHOR SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT |
---|
24 | n/a | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A |
---|
25 | n/a | PARTICULAR PURPOSE. THE CODE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, |
---|
26 | n/a | AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE, |
---|
27 | n/a | SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. |
---|
28 | n/a | """ |
---|
29 | n/a | |
---|
30 | n/a | __author__ = "Steve Purcell (stephen_purcell@yahoo.com)" |
---|
31 | n/a | |
---|
32 | n/a | import sys |
---|
33 | n/a | import traceback |
---|
34 | n/a | import unittest |
---|
35 | n/a | |
---|
36 | n/a | import tkinter as tk |
---|
37 | n/a | from tkinter import messagebox |
---|
38 | n/a | from tkinter import filedialog |
---|
39 | n/a | from tkinter import simpledialog |
---|
40 | n/a | |
---|
41 | n/a | |
---|
42 | n/a | |
---|
43 | n/a | |
---|
44 | n/a | ############################################################################## |
---|
45 | n/a | # GUI framework classes |
---|
46 | n/a | ############################################################################## |
---|
47 | n/a | |
---|
48 | n/a | class BaseGUITestRunner(object): |
---|
49 | n/a | """Subclass this class to create a GUI TestRunner that uses a specific |
---|
50 | n/a | windowing toolkit. The class takes care of running tests in the correct |
---|
51 | n/a | manner, and making callbacks to the derived class to obtain information |
---|
52 | n/a | or signal that events have occurred. |
---|
53 | n/a | """ |
---|
54 | n/a | def __init__(self, *args, **kwargs): |
---|
55 | n/a | self.currentResult = None |
---|
56 | n/a | self.running = 0 |
---|
57 | n/a | self.__rollbackImporter = None |
---|
58 | n/a | self.__rollbackImporter = RollbackImporter() |
---|
59 | n/a | self.test_suite = None |
---|
60 | n/a | |
---|
61 | n/a | #test discovery variables |
---|
62 | n/a | self.directory_to_read = '' |
---|
63 | n/a | self.top_level_dir = '' |
---|
64 | n/a | self.test_file_glob_pattern = 'test*.py' |
---|
65 | n/a | |
---|
66 | n/a | self.initGUI(*args, **kwargs) |
---|
67 | n/a | |
---|
68 | n/a | def errorDialog(self, title, message): |
---|
69 | n/a | "Override to display an error arising from GUI usage" |
---|
70 | n/a | pass |
---|
71 | n/a | |
---|
72 | n/a | def getDirectoryToDiscover(self): |
---|
73 | n/a | "Override to prompt user for directory to perform test discovery" |
---|
74 | n/a | pass |
---|
75 | n/a | |
---|
76 | n/a | def runClicked(self): |
---|
77 | n/a | "To be called in response to user choosing to run a test" |
---|
78 | n/a | if self.running: return |
---|
79 | n/a | if not self.test_suite: |
---|
80 | n/a | self.errorDialog("Test Discovery", "You discover some tests first!") |
---|
81 | n/a | return |
---|
82 | n/a | self.currentResult = GUITestResult(self) |
---|
83 | n/a | self.totalTests = self.test_suite.countTestCases() |
---|
84 | n/a | self.running = 1 |
---|
85 | n/a | self.notifyRunning() |
---|
86 | n/a | self.test_suite.run(self.currentResult) |
---|
87 | n/a | self.running = 0 |
---|
88 | n/a | self.notifyStopped() |
---|
89 | n/a | |
---|
90 | n/a | def stopClicked(self): |
---|
91 | n/a | "To be called in response to user stopping the running of a test" |
---|
92 | n/a | if self.currentResult: |
---|
93 | n/a | self.currentResult.stop() |
---|
94 | n/a | |
---|
95 | n/a | def discoverClicked(self): |
---|
96 | n/a | self.__rollbackImporter.rollbackImports() |
---|
97 | n/a | directory = self.getDirectoryToDiscover() |
---|
98 | n/a | if not directory: |
---|
99 | n/a | return |
---|
100 | n/a | self.directory_to_read = directory |
---|
101 | n/a | try: |
---|
102 | n/a | # Explicitly use 'None' value if no top level directory is |
---|
103 | n/a | # specified (indicated by empty string) as discover() explicitly |
---|
104 | n/a | # checks for a 'None' to determine if no tld has been specified |
---|
105 | n/a | top_level_dir = self.top_level_dir or None |
---|
106 | n/a | tests = unittest.defaultTestLoader.discover(directory, self.test_file_glob_pattern, top_level_dir) |
---|
107 | n/a | self.test_suite = tests |
---|
108 | n/a | except: |
---|
109 | n/a | exc_type, exc_value, exc_tb = sys.exc_info() |
---|
110 | n/a | traceback.print_exception(*sys.exc_info()) |
---|
111 | n/a | self.errorDialog("Unable to run test '%s'" % directory, |
---|
112 | n/a | "Error loading specified test: %s, %s" % (exc_type, exc_value)) |
---|
113 | n/a | return |
---|
114 | n/a | self.notifyTestsDiscovered(self.test_suite) |
---|
115 | n/a | |
---|
116 | n/a | # Required callbacks |
---|
117 | n/a | |
---|
118 | n/a | def notifyTestsDiscovered(self, test_suite): |
---|
119 | n/a | "Override to display information about the suite of discovered tests" |
---|
120 | n/a | pass |
---|
121 | n/a | |
---|
122 | n/a | def notifyRunning(self): |
---|
123 | n/a | "Override to set GUI in 'running' mode, enabling 'stop' button etc." |
---|
124 | n/a | pass |
---|
125 | n/a | |
---|
126 | n/a | def notifyStopped(self): |
---|
127 | n/a | "Override to set GUI in 'stopped' mode, enabling 'run' button etc." |
---|
128 | n/a | pass |
---|
129 | n/a | |
---|
130 | n/a | def notifyTestFailed(self, test, err): |
---|
131 | n/a | "Override to indicate that a test has just failed" |
---|
132 | n/a | pass |
---|
133 | n/a | |
---|
134 | n/a | def notifyTestErrored(self, test, err): |
---|
135 | n/a | "Override to indicate that a test has just errored" |
---|
136 | n/a | pass |
---|
137 | n/a | |
---|
138 | n/a | def notifyTestSkipped(self, test, reason): |
---|
139 | n/a | "Override to indicate that test was skipped" |
---|
140 | n/a | pass |
---|
141 | n/a | |
---|
142 | n/a | def notifyTestFailedExpectedly(self, test, err): |
---|
143 | n/a | "Override to indicate that test has just failed expectedly" |
---|
144 | n/a | pass |
---|
145 | n/a | |
---|
146 | n/a | def notifyTestStarted(self, test): |
---|
147 | n/a | "Override to indicate that a test is about to run" |
---|
148 | n/a | pass |
---|
149 | n/a | |
---|
150 | n/a | def notifyTestFinished(self, test): |
---|
151 | n/a | """Override to indicate that a test has finished (it may already have |
---|
152 | n/a | failed or errored)""" |
---|
153 | n/a | pass |
---|
154 | n/a | |
---|
155 | n/a | |
---|
156 | n/a | class GUITestResult(unittest.TestResult): |
---|
157 | n/a | """A TestResult that makes callbacks to its associated GUI TestRunner. |
---|
158 | n/a | Used by BaseGUITestRunner. Need not be created directly. |
---|
159 | n/a | """ |
---|
160 | n/a | def __init__(self, callback): |
---|
161 | n/a | unittest.TestResult.__init__(self) |
---|
162 | n/a | self.callback = callback |
---|
163 | n/a | |
---|
164 | n/a | def addError(self, test, err): |
---|
165 | n/a | unittest.TestResult.addError(self, test, err) |
---|
166 | n/a | self.callback.notifyTestErrored(test, err) |
---|
167 | n/a | |
---|
168 | n/a | def addFailure(self, test, err): |
---|
169 | n/a | unittest.TestResult.addFailure(self, test, err) |
---|
170 | n/a | self.callback.notifyTestFailed(test, err) |
---|
171 | n/a | |
---|
172 | n/a | def addSkip(self, test, reason): |
---|
173 | n/a | super(GUITestResult,self).addSkip(test, reason) |
---|
174 | n/a | self.callback.notifyTestSkipped(test, reason) |
---|
175 | n/a | |
---|
176 | n/a | def addExpectedFailure(self, test, err): |
---|
177 | n/a | super(GUITestResult,self).addExpectedFailure(test, err) |
---|
178 | n/a | self.callback.notifyTestFailedExpectedly(test, err) |
---|
179 | n/a | |
---|
180 | n/a | def stopTest(self, test): |
---|
181 | n/a | unittest.TestResult.stopTest(self, test) |
---|
182 | n/a | self.callback.notifyTestFinished(test) |
---|
183 | n/a | |
---|
184 | n/a | def startTest(self, test): |
---|
185 | n/a | unittest.TestResult.startTest(self, test) |
---|
186 | n/a | self.callback.notifyTestStarted(test) |
---|
187 | n/a | |
---|
188 | n/a | |
---|
189 | n/a | class RollbackImporter: |
---|
190 | n/a | """This tricky little class is used to make sure that modules under test |
---|
191 | n/a | will be reloaded the next time they are imported. |
---|
192 | n/a | """ |
---|
193 | n/a | def __init__(self): |
---|
194 | n/a | self.previousModules = sys.modules.copy() |
---|
195 | n/a | |
---|
196 | n/a | def rollbackImports(self): |
---|
197 | n/a | for modname in sys.modules.copy().keys(): |
---|
198 | n/a | if not modname in self.previousModules: |
---|
199 | n/a | # Force reload when modname next imported |
---|
200 | n/a | del(sys.modules[modname]) |
---|
201 | n/a | |
---|
202 | n/a | |
---|
203 | n/a | ############################################################################## |
---|
204 | n/a | # Tkinter GUI |
---|
205 | n/a | ############################################################################## |
---|
206 | n/a | |
---|
207 | n/a | class DiscoverSettingsDialog(simpledialog.Dialog): |
---|
208 | n/a | """ |
---|
209 | n/a | Dialog box for prompting test discovery settings |
---|
210 | n/a | """ |
---|
211 | n/a | |
---|
212 | n/a | def __init__(self, master, top_level_dir, test_file_glob_pattern, *args, **kwargs): |
---|
213 | n/a | self.top_level_dir = top_level_dir |
---|
214 | n/a | self.dirVar = tk.StringVar() |
---|
215 | n/a | self.dirVar.set(top_level_dir) |
---|
216 | n/a | |
---|
217 | n/a | self.test_file_glob_pattern = test_file_glob_pattern |
---|
218 | n/a | self.testPatternVar = tk.StringVar() |
---|
219 | n/a | self.testPatternVar.set(test_file_glob_pattern) |
---|
220 | n/a | |
---|
221 | n/a | simpledialog.Dialog.__init__(self, master, title="Discover Settings", |
---|
222 | n/a | *args, **kwargs) |
---|
223 | n/a | |
---|
224 | n/a | def body(self, master): |
---|
225 | n/a | tk.Label(master, text="Top Level Directory").grid(row=0) |
---|
226 | n/a | self.e1 = tk.Entry(master, textvariable=self.dirVar) |
---|
227 | n/a | self.e1.grid(row = 0, column=1) |
---|
228 | n/a | tk.Button(master, text="...", |
---|
229 | n/a | command=lambda: self.selectDirClicked(master)).grid(row=0,column=3) |
---|
230 | n/a | |
---|
231 | n/a | tk.Label(master, text="Test File Pattern").grid(row=1) |
---|
232 | n/a | self.e2 = tk.Entry(master, textvariable = self.testPatternVar) |
---|
233 | n/a | self.e2.grid(row = 1, column=1) |
---|
234 | n/a | return None |
---|
235 | n/a | |
---|
236 | n/a | def selectDirClicked(self, master): |
---|
237 | n/a | dir_path = filedialog.askdirectory(parent=master) |
---|
238 | n/a | if dir_path: |
---|
239 | n/a | self.dirVar.set(dir_path) |
---|
240 | n/a | |
---|
241 | n/a | def apply(self): |
---|
242 | n/a | self.top_level_dir = self.dirVar.get() |
---|
243 | n/a | self.test_file_glob_pattern = self.testPatternVar.get() |
---|
244 | n/a | |
---|
245 | n/a | class TkTestRunner(BaseGUITestRunner): |
---|
246 | n/a | """An implementation of BaseGUITestRunner using Tkinter. |
---|
247 | n/a | """ |
---|
248 | n/a | def initGUI(self, root, initialTestName): |
---|
249 | n/a | """Set up the GUI inside the given root window. The test name entry |
---|
250 | n/a | field will be pre-filled with the given initialTestName. |
---|
251 | n/a | """ |
---|
252 | n/a | self.root = root |
---|
253 | n/a | |
---|
254 | n/a | self.statusVar = tk.StringVar() |
---|
255 | n/a | self.statusVar.set("Idle") |
---|
256 | n/a | |
---|
257 | n/a | #tk vars for tracking counts of test result types |
---|
258 | n/a | self.runCountVar = tk.IntVar() |
---|
259 | n/a | self.failCountVar = tk.IntVar() |
---|
260 | n/a | self.errorCountVar = tk.IntVar() |
---|
261 | n/a | self.skipCountVar = tk.IntVar() |
---|
262 | n/a | self.expectFailCountVar = tk.IntVar() |
---|
263 | n/a | self.remainingCountVar = tk.IntVar() |
---|
264 | n/a | |
---|
265 | n/a | self.top = tk.Frame() |
---|
266 | n/a | self.top.pack(fill=tk.BOTH, expand=1) |
---|
267 | n/a | self.createWidgets() |
---|
268 | n/a | |
---|
269 | n/a | def getDirectoryToDiscover(self): |
---|
270 | n/a | return filedialog.askdirectory() |
---|
271 | n/a | |
---|
272 | n/a | def settingsClicked(self): |
---|
273 | n/a | d = DiscoverSettingsDialog(self.top, self.top_level_dir, self.test_file_glob_pattern) |
---|
274 | n/a | self.top_level_dir = d.top_level_dir |
---|
275 | n/a | self.test_file_glob_pattern = d.test_file_glob_pattern |
---|
276 | n/a | |
---|
277 | n/a | def notifyTestsDiscovered(self, test_suite): |
---|
278 | n/a | discovered = test_suite.countTestCases() |
---|
279 | n/a | self.runCountVar.set(0) |
---|
280 | n/a | self.failCountVar.set(0) |
---|
281 | n/a | self.errorCountVar.set(0) |
---|
282 | n/a | self.remainingCountVar.set(discovered) |
---|
283 | n/a | self.progressBar.setProgressFraction(0.0) |
---|
284 | n/a | self.errorListbox.delete(0, tk.END) |
---|
285 | n/a | self.statusVar.set("Discovering tests from %s. Found: %s" % |
---|
286 | n/a | (self.directory_to_read, discovered)) |
---|
287 | n/a | self.stopGoButton['state'] = tk.NORMAL |
---|
288 | n/a | |
---|
289 | n/a | def createWidgets(self): |
---|
290 | n/a | """Creates and packs the various widgets. |
---|
291 | n/a | |
---|
292 | n/a | Why is it that GUI code always ends up looking a mess, despite all the |
---|
293 | n/a | best intentions to keep it tidy? Answers on a postcard, please. |
---|
294 | n/a | """ |
---|
295 | n/a | # Status bar |
---|
296 | n/a | statusFrame = tk.Frame(self.top, relief=tk.SUNKEN, borderwidth=2) |
---|
297 | n/a | statusFrame.pack(anchor=tk.SW, fill=tk.X, side=tk.BOTTOM) |
---|
298 | n/a | tk.Label(statusFrame, width=1, textvariable=self.statusVar).pack(side=tk.TOP, fill=tk.X) |
---|
299 | n/a | |
---|
300 | n/a | # Area to enter name of test to run |
---|
301 | n/a | leftFrame = tk.Frame(self.top, borderwidth=3) |
---|
302 | n/a | leftFrame.pack(fill=tk.BOTH, side=tk.LEFT, anchor=tk.NW, expand=1) |
---|
303 | n/a | suiteNameFrame = tk.Frame(leftFrame, borderwidth=3) |
---|
304 | n/a | suiteNameFrame.pack(fill=tk.X) |
---|
305 | n/a | |
---|
306 | n/a | # Progress bar |
---|
307 | n/a | progressFrame = tk.Frame(leftFrame, relief=tk.GROOVE, borderwidth=2) |
---|
308 | n/a | progressFrame.pack(fill=tk.X, expand=0, anchor=tk.NW) |
---|
309 | n/a | tk.Label(progressFrame, text="Progress:").pack(anchor=tk.W) |
---|
310 | n/a | self.progressBar = ProgressBar(progressFrame, relief=tk.SUNKEN, |
---|
311 | n/a | borderwidth=2) |
---|
312 | n/a | self.progressBar.pack(fill=tk.X, expand=1) |
---|
313 | n/a | |
---|
314 | n/a | |
---|
315 | n/a | # Area with buttons to start/stop tests and quit |
---|
316 | n/a | buttonFrame = tk.Frame(self.top, borderwidth=3) |
---|
317 | n/a | buttonFrame.pack(side=tk.LEFT, anchor=tk.NW, fill=tk.Y) |
---|
318 | n/a | |
---|
319 | n/a | tk.Button(buttonFrame, text="Discover Tests", |
---|
320 | n/a | command=self.discoverClicked).pack(fill=tk.X) |
---|
321 | n/a | |
---|
322 | n/a | |
---|
323 | n/a | self.stopGoButton = tk.Button(buttonFrame, text="Start", |
---|
324 | n/a | command=self.runClicked, state=tk.DISABLED) |
---|
325 | n/a | self.stopGoButton.pack(fill=tk.X) |
---|
326 | n/a | |
---|
327 | n/a | tk.Button(buttonFrame, text="Close", |
---|
328 | n/a | command=self.top.quit).pack(side=tk.BOTTOM, fill=tk.X) |
---|
329 | n/a | tk.Button(buttonFrame, text="Settings", |
---|
330 | n/a | command=self.settingsClicked).pack(side=tk.BOTTOM, fill=tk.X) |
---|
331 | n/a | |
---|
332 | n/a | # Area with labels reporting results |
---|
333 | n/a | for label, var in (('Run:', self.runCountVar), |
---|
334 | n/a | ('Failures:', self.failCountVar), |
---|
335 | n/a | ('Errors:', self.errorCountVar), |
---|
336 | n/a | ('Skipped:', self.skipCountVar), |
---|
337 | n/a | ('Expected Failures:', self.expectFailCountVar), |
---|
338 | n/a | ('Remaining:', self.remainingCountVar), |
---|
339 | n/a | ): |
---|
340 | n/a | tk.Label(progressFrame, text=label).pack(side=tk.LEFT) |
---|
341 | n/a | tk.Label(progressFrame, textvariable=var, |
---|
342 | n/a | foreground="blue").pack(side=tk.LEFT, fill=tk.X, |
---|
343 | n/a | expand=1, anchor=tk.W) |
---|
344 | n/a | |
---|
345 | n/a | # List box showing errors and failures |
---|
346 | n/a | tk.Label(leftFrame, text="Failures and errors:").pack(anchor=tk.W) |
---|
347 | n/a | listFrame = tk.Frame(leftFrame, relief=tk.SUNKEN, borderwidth=2) |
---|
348 | n/a | listFrame.pack(fill=tk.BOTH, anchor=tk.NW, expand=1) |
---|
349 | n/a | self.errorListbox = tk.Listbox(listFrame, foreground='red', |
---|
350 | n/a | selectmode=tk.SINGLE, |
---|
351 | n/a | selectborderwidth=0) |
---|
352 | n/a | self.errorListbox.pack(side=tk.LEFT, fill=tk.BOTH, expand=1, |
---|
353 | n/a | anchor=tk.NW) |
---|
354 | n/a | listScroll = tk.Scrollbar(listFrame, command=self.errorListbox.yview) |
---|
355 | n/a | listScroll.pack(side=tk.LEFT, fill=tk.Y, anchor=tk.N) |
---|
356 | n/a | self.errorListbox.bind("<Double-1>", |
---|
357 | n/a | lambda e, self=self: self.showSelectedError()) |
---|
358 | n/a | self.errorListbox.configure(yscrollcommand=listScroll.set) |
---|
359 | n/a | |
---|
360 | n/a | def errorDialog(self, title, message): |
---|
361 | n/a | messagebox.showerror(parent=self.root, title=title, |
---|
362 | n/a | message=message) |
---|
363 | n/a | |
---|
364 | n/a | def notifyRunning(self): |
---|
365 | n/a | self.runCountVar.set(0) |
---|
366 | n/a | self.failCountVar.set(0) |
---|
367 | n/a | self.errorCountVar.set(0) |
---|
368 | n/a | self.remainingCountVar.set(self.totalTests) |
---|
369 | n/a | self.errorInfo = [] |
---|
370 | n/a | while self.errorListbox.size(): |
---|
371 | n/a | self.errorListbox.delete(0) |
---|
372 | n/a | #Stopping seems not to work, so simply disable the start button |
---|
373 | n/a | #self.stopGoButton.config(command=self.stopClicked, text="Stop") |
---|
374 | n/a | self.stopGoButton.config(state=tk.DISABLED) |
---|
375 | n/a | self.progressBar.setProgressFraction(0.0) |
---|
376 | n/a | self.top.update_idletasks() |
---|
377 | n/a | |
---|
378 | n/a | def notifyStopped(self): |
---|
379 | n/a | self.stopGoButton.config(state=tk.DISABLED) |
---|
380 | n/a | #self.stopGoButton.config(command=self.runClicked, text="Start") |
---|
381 | n/a | self.statusVar.set("Idle") |
---|
382 | n/a | |
---|
383 | n/a | def notifyTestStarted(self, test): |
---|
384 | n/a | self.statusVar.set(str(test)) |
---|
385 | n/a | self.top.update_idletasks() |
---|
386 | n/a | |
---|
387 | n/a | def notifyTestFailed(self, test, err): |
---|
388 | n/a | self.failCountVar.set(1 + self.failCountVar.get()) |
---|
389 | n/a | self.errorListbox.insert(tk.END, "Failure: %s" % test) |
---|
390 | n/a | self.errorInfo.append((test,err)) |
---|
391 | n/a | |
---|
392 | n/a | def notifyTestErrored(self, test, err): |
---|
393 | n/a | self.errorCountVar.set(1 + self.errorCountVar.get()) |
---|
394 | n/a | self.errorListbox.insert(tk.END, "Error: %s" % test) |
---|
395 | n/a | self.errorInfo.append((test,err)) |
---|
396 | n/a | |
---|
397 | n/a | def notifyTestSkipped(self, test, reason): |
---|
398 | n/a | super(TkTestRunner, self).notifyTestSkipped(test, reason) |
---|
399 | n/a | self.skipCountVar.set(1 + self.skipCountVar.get()) |
---|
400 | n/a | |
---|
401 | n/a | def notifyTestFailedExpectedly(self, test, err): |
---|
402 | n/a | super(TkTestRunner, self).notifyTestFailedExpectedly(test, err) |
---|
403 | n/a | self.expectFailCountVar.set(1 + self.expectFailCountVar.get()) |
---|
404 | n/a | |
---|
405 | n/a | |
---|
406 | n/a | def notifyTestFinished(self, test): |
---|
407 | n/a | self.remainingCountVar.set(self.remainingCountVar.get() - 1) |
---|
408 | n/a | self.runCountVar.set(1 + self.runCountVar.get()) |
---|
409 | n/a | fractionDone = float(self.runCountVar.get())/float(self.totalTests) |
---|
410 | n/a | fillColor = len(self.errorInfo) and "red" or "green" |
---|
411 | n/a | self.progressBar.setProgressFraction(fractionDone, fillColor) |
---|
412 | n/a | |
---|
413 | n/a | def showSelectedError(self): |
---|
414 | n/a | selection = self.errorListbox.curselection() |
---|
415 | n/a | if not selection: return |
---|
416 | n/a | selected = int(selection[0]) |
---|
417 | n/a | txt = self.errorListbox.get(selected) |
---|
418 | n/a | window = tk.Toplevel(self.root) |
---|
419 | n/a | window.title(txt) |
---|
420 | n/a | window.protocol('WM_DELETE_WINDOW', window.quit) |
---|
421 | n/a | test, error = self.errorInfo[selected] |
---|
422 | n/a | tk.Label(window, text=str(test), |
---|
423 | n/a | foreground="red", justify=tk.LEFT).pack(anchor=tk.W) |
---|
424 | n/a | tracebackLines = traceback.format_exception(*error) |
---|
425 | n/a | tracebackText = "".join(tracebackLines) |
---|
426 | n/a | tk.Label(window, text=tracebackText, justify=tk.LEFT).pack() |
---|
427 | n/a | tk.Button(window, text="Close", |
---|
428 | n/a | command=window.quit).pack(side=tk.BOTTOM) |
---|
429 | n/a | window.bind('<Key-Return>', lambda e, w=window: w.quit()) |
---|
430 | n/a | window.mainloop() |
---|
431 | n/a | window.destroy() |
---|
432 | n/a | |
---|
433 | n/a | |
---|
434 | n/a | class ProgressBar(tk.Frame): |
---|
435 | n/a | """A simple progress bar that shows a percentage progress in |
---|
436 | n/a | the given colour.""" |
---|
437 | n/a | |
---|
438 | n/a | def __init__(self, *args, **kwargs): |
---|
439 | n/a | tk.Frame.__init__(self, *args, **kwargs) |
---|
440 | n/a | self.canvas = tk.Canvas(self, height='20', width='60', |
---|
441 | n/a | background='white', borderwidth=3) |
---|
442 | n/a | self.canvas.pack(fill=tk.X, expand=1) |
---|
443 | n/a | self.rect = self.text = None |
---|
444 | n/a | self.canvas.bind('<Configure>', self.paint) |
---|
445 | n/a | self.setProgressFraction(0.0) |
---|
446 | n/a | |
---|
447 | n/a | def setProgressFraction(self, fraction, color='blue'): |
---|
448 | n/a | self.fraction = fraction |
---|
449 | n/a | self.color = color |
---|
450 | n/a | self.paint() |
---|
451 | n/a | self.canvas.update_idletasks() |
---|
452 | n/a | |
---|
453 | n/a | def paint(self, *args): |
---|
454 | n/a | totalWidth = self.canvas.winfo_width() |
---|
455 | n/a | width = int(self.fraction * float(totalWidth)) |
---|
456 | n/a | height = self.canvas.winfo_height() |
---|
457 | n/a | if self.rect is not None: self.canvas.delete(self.rect) |
---|
458 | n/a | if self.text is not None: self.canvas.delete(self.text) |
---|
459 | n/a | self.rect = self.canvas.create_rectangle(0, 0, width, height, |
---|
460 | n/a | fill=self.color) |
---|
461 | n/a | percentString = "%3.0f%%" % (100.0 * self.fraction) |
---|
462 | n/a | self.text = self.canvas.create_text(totalWidth/2, height/2, |
---|
463 | n/a | anchor=tk.CENTER, |
---|
464 | n/a | text=percentString) |
---|
465 | n/a | |
---|
466 | n/a | def main(initialTestName=""): |
---|
467 | n/a | root = tk.Tk() |
---|
468 | n/a | root.title("PyUnit") |
---|
469 | n/a | runner = TkTestRunner(root, initialTestName) |
---|
470 | n/a | root.protocol('WM_DELETE_WINDOW', root.quit) |
---|
471 | n/a | root.mainloop() |
---|
472 | n/a | |
---|
473 | n/a | |
---|
474 | n/a | if __name__ == '__main__': |
---|
475 | n/a | if len(sys.argv) == 2: |
---|
476 | n/a | main(sys.argv[1]) |
---|
477 | n/a | else: |
---|
478 | n/a | main() |
---|