rununit.py (1805B)
1#!/usr/bin/env python 2'''Script to run all tests using python "unittest" module''' 3 4__author__ = "Miki Tebeka <miki.tebeka@zoran.com>" 5 6from unittest import TestCase, main, makeSuite, TestSuite 7from os import popen, environ, remove 8from glob import glob 9from sys import executable, argv 10from os.path import isfile, basename, splitext 11 12# Add path to lex.py and yacc.py 13environ["PYTHONPATH"] = ".." 14 15class PLYTest(TestCase): 16 '''General test case for PLY test''' 17 def _runtest(self, filename): 18 '''Run a single test file an compare result''' 19 exp_file = filename.replace(".py", ".exp") 20 self.failUnless(isfile(exp_file), "can't find %s" % exp_file) 21 pipe = popen("%s %s 2>&1" % (executable, filename)) 22 out = pipe.read().strip() 23 self.failUnlessEqual(out, open(exp_file).read().strip()) 24 25 26class LexText(PLYTest): 27 '''Testing Lex''' 28 pass 29 30class YaccTest(PLYTest): 31 '''Testing Yacc''' 32 33 def tearDown(self): 34 '''Cleanup parsetab.py[c] file''' 35 for ext in (".py", ".pyc"): 36 fname = "parsetab%s" % ext 37 if isfile(fname): 38 remove(fname) 39 40def add_test(klass, filename): 41 '''Add a test to TestCase class''' 42 def t(self): 43 self._runtest(filename) 44 # Test name is test_FILENAME without the ./ and without the .py 45 setattr(klass, "test_%s" % (splitext(basename(filename))[0]), t) 46 47# Add lex tests 48for file in glob("./lex_*.py"): 49 add_test(LexText, file) 50lex_suite = makeSuite(LexText, "test_") 51 52# Add yacc tests 53for file in glob("./yacc_*.py"): 54 add_test(YaccTest, file) 55yacc_suite = makeSuite(YaccTest, "test_") 56 57# All tests suite 58test_suite = TestSuite((lex_suite, yacc_suite)) 59 60if __name__ == "__main__": 61 main() 62