[jbig2-cvs] rev 452 - trunk

giles at ghostscript.com giles at ghostscript.com
Mon Feb 5 14:59:10 PST 2007


Author: giles
Date: 2007-02-05 14:59:10 -0800 (Mon, 05 Feb 2007)
New Revision: 452

Modified:
   trunk/test_jbig2dec.py
Log:
Rewrite the test script to use a custom test class. 

Previously we used the python unittest module, but we don't use most of 
its features, and it's quite weak for this kind of file-based external 
testing. So we lose on using a familiar api, but this doesn't add much 
code, and we can now add long-desired features like 'xfail' results.

This grew out of work revising the Ghostscript test code for 
parallelization.


Modified: trunk/test_jbig2dec.py
===================================================================
--- trunk/test_jbig2dec.py	2006-07-27 01:12:30 UTC (rev 451)
+++ trunk/test_jbig2dec.py	2007-02-05 22:59:10 UTC (rev 452)
@@ -4,10 +4,59 @@
 # $Id$
 
 import os, re
-import unittest
+import sys, time
 
-class KnownFileHash(unittest.TestCase):
+class SelfTest:
+  'generic class for self tests'
+  def __init__(self):
+    self.result = 'unrun'
+    self.msg = ''
+  def shortDescription(self):
+    'returns a short name for the test'
+    return "generic self test"
+  def runTest(self):
+    'call this to execute the test'
+    pass
+  def fail(self, msg=None):
+    self.result = 'FAIL'
+    self.msg = msg
+  def failIf(self, check, msg=None):
+    if check: self.fail(msg)
+  def assertEqual(self, a, b, msg=None):
+    if a != b: self.fail(msg)
 
+class SelfTestSuite:
+  'generic class for running a collection of SelfTest instances'
+  def __init__(self, stream=sys.stderr):
+    self.stream = stream
+    self.tests = []
+    self.fails = []
+    self.xfails = []
+    self.errors = []
+  def addTest(self, test):
+    self.tests.append(test)
+  def run(self):
+    starttime = time.time()
+    for test in self.tests:
+      self.stream.write("%s ... " % test.shortDescription())
+      test.result = 'ok'
+      test.runTest()
+      if test.result != 'ok':
+        self.fails.append(test)
+      self.stream.write("%s\n" % test.result)
+    stoptime = time.time()
+    self.stream.write('-'*72 + '\n')
+    self.stream.write('ran %d tests in %.3f seconds\n\n' % 
+	(len(self.tests), stoptime - starttime))
+    if len(self.fails):
+      self.stream.write('FAILED %d of %d tests\n' % 
+	(len(self.fails),len(self.tests)))
+    else:
+      self.stream.write('PASSED all %d tests\n' % len(self.tests))
+
+class KnownFileHash(SelfTest):
+  'self test to check for correct decode of known test files'
+
   # hashes of known test inputs
   known_042_DECODED = "ebfdf6e2fc5ff3ee2271c2fa19de0e52712046e8"
   # we do not have correct hashes for these
@@ -101,10 +150,10 @@
                         "ff373f070f5f405b732c53ffffff087eff22ff5b") )
 
   def __init__(self, file, file_hash, decode_hash):
+    SelfTest.__init__(self)
     self.file = file
     self.file_hash = file_hash
     self.decode_hash = decode_hash
-    unittest.TestCase.__init__(self)
 
   def shortDescription(self):
     return "Checking '%s' for correct decoded document hash" % self.file
@@ -126,7 +175,7 @@
         return
     self.fail('document hash was not found in the output')
 
-suite = unittest.TestSuite()
+suite = SelfTestSuite()
 for filename, file_hash, decode_hash in KnownFileHash.known_hashes:
   # only add tests for files we can find
   if not os.access(filename, os.R_OK): continue
@@ -135,8 +184,5 @@
 
 # run the defined tests if we're called as a script
 if __name__ == "__main__":
-    import sys
-    verbosity = 2
-    runner = unittest.TextTestRunner(verbosity=verbosity)
-    result = runner.run(suite)
-    sys.exit(not result.wasSuccessful())
+    result = suite.run()
+    sys.exit(not result)



More information about the jbig2-cvs mailing list