1 from __future__ import with_statement
4 from distutils.core import Command
5 from unittest import TextTestRunner, TestLoader
7 from os.path import dirname, join, realpath, splitext, basename, walk
11 class AutoTestMetaclass(type):
13 def __new__(cls, name, bases, class_dict):
14 test_dir = class_dict.pop('TEST_DIR')
15 path = realpath( join(dirname(__file__), 'files', test_dir) )
17 for file in listdir(path):
18 base, ext = splitext(file)
22 class_dict['test_'+base] = cls.make_test_runner(base, \
23 join(path, base +'.xml'), join(path, base + '.out') )
25 return type.__new__(cls, name, bases, class_dict)
28 def make_test_runner(name, inputf, outputf):
30 with open(inputf, 'rb') as ifd:
31 with codecs.open(outputf, 'rb', encoding='utf-8') as ofd:
32 self.run_auto_test(ifd.read(), ofd.read())
36 def get_file_path(dir_name, file_name):
37 return realpath(join(dirname(__file__), 'files', dir_name, file_name))
39 class TestCommand(Command):
42 def initialize_options(self):
43 self._dir = os.getcwd()
45 def finalize_options(self):
50 Finds all the tests modules in tests/, and runs them.
53 for t in glob(join(self._dir, 'tests', '*.py')):
54 module_name = splitext(basename(t))[0]
55 if module_name.startswith('test'):
56 testfiles.append('.'.join(['tests', module_name])
59 tests = TestLoader().loadTestsFromNames(testfiles)
60 t = TextTestRunner(verbosity=2)