bh@12: # Copyright (C) 2007 by Intevation GmbH bh@12: # Authors: bh@12: # Bernhard Herzog bh@12: # bh@12: # This program is free software under the GPL (>=v2) bh@12: # Read the file COPYING coming with the software for details. bh@12: bh@12: """ bh@12: Main entry point for the test suite. bh@12: bh@12: Just run this file as a python script to execute all tests bh@12: """ bh@12: bh@12: import os bh@12: import sys bh@12: import unittest bh@12: import optparse bh@12: bh@12: test_dir = os.path.dirname(__file__) bh@12: sys.path.append(os.path.join(test_dir, os.pardir)) bh@12: bh@12: def find_test_modules(dirname, package = None): bh@12: """Return a list the names of the test modules in the directory dirname bh@12: bh@12: The return value is a list of names that can be passed to bh@12: unittest.defaultTestLoader.loadTestsFromNames. Each name of the bh@12: list is the name of a pure python module, one for each file in bh@12: dirname whose name starts with 'test' and ends with '.py'. bh@12: bh@12: The optional parameter package should be the name of the python bh@12: package whose directory is dirname. If package is given all names bh@12: in the returned list will be prefixed with package and a dot. bh@12: """ bh@12: if package: bh@12: prefix = package + "." bh@12: else: bh@12: prefix = "" bh@12: bh@12: suffix = ".py" bh@12: return [prefix + name[:-len(suffix)] bh@12: for name in os.listdir(dirname) bh@12: if name.startswith("test") and name.endswith(suffix)] bh@12: bh@12: bh@12: def main(): bh@12: """Run all the tests in the test suite""" bh@12: bh@12: parser = optparse.OptionParser() bh@12: parser.set_defaults(verbosity=1) bh@12: parser.add_option("-v", "--verbose", action="store_const", const=2, bh@12: dest="verbosity") bh@12: opts, rest = parser.parse_args() bh@12: bh@12: # Build the list of test names. If names were given on the command bh@12: # line, run exactly those. Othwerwise build a default list of bh@12: # names. bh@12: if rest: bh@12: names = rest bh@12: else: bh@12: # All Python files starting with 'test' in the current directory bh@12: # and some directories in Extensions contain test cases. bh@12: # FIXME: It should be possible to run runtests.py even when not in bh@12: # the test directory bh@12: names = find_test_modules(test_dir) bh@12: suite = unittest.defaultTestLoader.loadTestsFromNames(names) bh@12: runner = unittest.TextTestRunner(verbosity=opts.verbosity) bh@12: result = runner.run(suite) bh@12: sys.exit(not result.wasSuccessful()) bh@12: bh@12: bh@12: if __name__ == "__main__": bh@12: main()