rev 452 - trunk
[email protected] Mon, 5 Feb 2007 14:59:10 -0800 (PST)
| Newsgroups | gmane.comp.printing.ghostscript.jbig2dec.cvs |
|---|---|
| Message-ID | <[email protected]> |
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.=20
Previously we used the python unittest module, but we don't use most of=20
its features, and it's quite weak for this kind of file-based external=20
testing. So we lose on using a familiar api, but this doesn't add much=20
code, and we can now add long-desired features like 'xfail' results.
This grew out of work revising the Ghostscript test code for=20
parallelization.
Modified: trunk/test_jbig2dec.py
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- 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$
=20
import os, re
-import unittest
+import sys, time
=20
-class KnownFileHash(unittest.TestCase):
+class SelfTest:
+ 'generic class for self tests'
+ def __init__(self):
+ self.result =3D 'unrun'
+ self.msg =3D ''
+ 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=3DNone):
+ self.result =3D 'FAIL'
+ self.msg =3D msg
+ def failIf(self, check, msg=3DNone):
+ if check: self.fail(msg)
+ def assertEqual(self, a, b, msg=3DNone):
+ if a !=3D b: self.fail(msg)
=20
+class SelfTestSuite:
+ 'generic class for running a collection of SelfTest instances'
+ def __init__(self, stream=3Dsys.stderr):
+ self.stream =3D stream
+ self.tests =3D []
+ self.fails =3D []
+ self.xfails =3D []
+ self.errors =3D []
+ def addTest(self, test):
+ self.tests.append(test)
+ def run(self):
+ starttime =3D time.time()
+ for test in self.tests:
+ self.stream.write("%s ... " % test.shortDescription())
+ test.result =3D 'ok'
+ test.runTest()
+ if test.result !=3D 'ok':
+ self.fails.append(test)
+ self.stream.write("%s\n" % test.result)
+ stoptime =3D time.time()
+ self.stream.write('-'*72 + '\n')
+ self.stream.write('ran %d tests in %.3f seconds\n\n' %=20
+ (len(self.tests), stoptime - starttime))
+ if len(self.fails):
+ self.stream.write('FAILED %d of %d tests\n' %=20
+ (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 =3D "ebfdf6e2fc5ff3ee2271c2fa19de0e52712046e8"
# we do not have correct hashes for these
@@ -101,10 +150,10 @@
"ff373f070f5f405b732c53ffffff087eff22ff5b") )
=20
def __init__(self, file, file_hash, decode_hash):
+ SelfTest.__init__(self)
self.file =3D file
self.file_hash =3D file_hash
self.decode_hash =3D decode_hash
- unittest.TestCase.__init__(self)
=20
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')
=20
-suite =3D unittest.TestSuite()
+suite =3D 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 @@
=20
# run the defined tests if we're called as a script
if __name__ =3D=3D "__main__":
- import sys
- verbosity =3D 2
- runner =3D unittest.TextTestRunner(verbosity=3Dverbosity)
- result =3D runner.run(suite)
- sys.exit(not result.wasSuccessful())
+ result =3D suite.run()
+ sys.exit(not result)