Index: tests/test_tokenizer.py =================================================================== --- tests/test_tokenizer.py (revision 215) +++ tests/test_tokenizer.py (working copy) @@ -7,12 +7,6 @@ import simplejson -#Allow us to import the parent module -os.chdir(os.path.split(os.path.abspath(__file__))[0]) -sys.path.insert(0, os.path.abspath(os.pardir)) - -import tokenizer - class TokenizerTestParser(object): def parse(self, stream, innerHTML=False): """Stream should be a stream of unicode bytes. Character encoding @@ -20,6 +14,7 @@ self.outputTokens = [] + import tokenizer self.tokenizer = tokenizer.HTMLTokenizer(self) self.tokenizer.tokenize(stream) @@ -100,8 +95,7 @@ yield (TestCase.runTokenizerTest, test['description'], test['input'], test['output']) -def main(): - failed = 0 +def buildTestSuite(): tests = 0 for func, desc, input, output in test_tokenizer(): tests += 1 @@ -111,7 +105,15 @@ testFunc.__doc__ = "\t".join([desc, str(input), str(output)]) instanceMethod = new.instancemethod(testFunc, None, TestCase) setattr(TestCase, testName, instanceMethod) + return unittest.TestLoader().loadTestsFromTestCase(TestCase) + +def main(): + buildTestSuite() unittest.main() if __name__ == "__main__": + #Allow us to import the parent module + os.chdir(os.path.split(os.path.abspath(__file__))[0]) + sys.path.insert(0, os.path.abspath(os.pardir)) + main() Index: tests/test_parser.py =================================================================== --- tests/test_parser.py (revision 215) +++ tests/test_parser.py (working copy) @@ -4,14 +4,7 @@ import StringIO import unittest import new -import traceback -#Allow us to import the parent module -os.chdir(os.path.split(os.path.abspath(__file__))[0]) -sys.path.insert(0, os.path.abspath(os.pardir)) - -import parser - def parseTestcase(testString): testString = testString.split("\n") try: @@ -48,68 +41,50 @@ rv.append(line[3:]) return "\n".join(rv) +class TestCase(unittest.TestCase): + def runParserTest(self, input, output, errors): + import parser + #XXX - move this out into the setup function + #concatenate all consecutive character tokens into a single token + p = parser.HTMLParser() + document = p.parse(StringIO.StringIO(input)) + self.assertEquals(output, convertTreeDump(document.printTree())) + def test_parser(): for filename in glob.glob('tree-construction/*.dat'): f = file(filename) test = [] lastLine = "" for line in f: - #Assume tests are seperated by a blank line + #Assume tests are separated by a blank line if not (line == "\n" and lastLine[0] == "|"): - #Strip out newlinw characters from the end of the string + #Strip out newline characters from the end of the string test.append(line[:-1]) else: input, output, errors = parseTestcase("\n".join(test)) - yield runParserTest, input, output, errors + yield TestCase.runParserTest, input, output, errors test = [] lastLine = line -def runParserTest(input, output, errors): - #XXX - move this out into the setup function - #concatenate all consecutive character tokens into a single token - p = parser.HTMLParser() - document = p.parse(StringIO.StringIO(input)) - try: - #Need a check on the number of parse errors here - assert output == convertTreeDump(document.printTree()) - except: - sys.stdout.write("input\n") - sys.stdout.write(input+"\n") - sys.stdout.write("expected output\n") - sys.stdout.write(output+"\n") - sys.stdout.write("received\n") - sys.stdout.write(convertTreeDump(document.printTree())+"\n") - sys.stdout.write("\n") - raise - -def main(): - failed = 0 +def buildTestSuite(): tests = 0 for func, input, output, errors in test_parser(): tests += 1 testName = 'test%d' % tests - try: - runParserTest(input, output, errors) - except AssertionError: - failed += 1 - except: - sys.stdout.write("ERROR\n") - sys.stdout.write(input+"\n") - sys.stderr = sys.stdout - traceback.print_exc() - sys.stderr = sys.__stderr__ - sys.stdout.write("\n") - failed += 1 + testFunc = lambda self, method=func, input=input, output=output, \ + errors=errors: method(self, input, output, errors) + testFunc.__doc__ = 'Parser %s: %s' % (testName, input) + instanceMethod = new.instancemethod(testFunc, None, TestCase) + setattr(TestCase, testName, instanceMethod) + return unittest.TestLoader().loadTestsFromTestCase(TestCase) - print "Ran %i tests, failed %i"%(tests, failed) +def main(): + buildTestSuite() + unittest.main() -# def testFunc(self, method=func, input=input, -# output=output, errors=errors): -# method(self, input, output, errors) -# testFunc.__doc__ = "\t".join([str(input)]) -# instanceMethod = new -# setattr(TestCase, testName, instanceMethod) -# unittest.main() - if __name__ == "__main__": + #Allow us to import the parent module + os.chdir(os.path.split(os.path.abspath(__file__))[0]) + sys.path.insert(0, os.path.abspath(os.pardir)) + main()