#!/usr/bin/python # # Runs all tests in the current directory # # Execute like: # python runalltests.py # import doctest import logging import os import shutil import signal import subprocess import sys import tempfile import unittest logging.basicConfig(level=logging.WARN, format="%(asctime)-15s %(name)s %(levelname)s: %(message)s", stream=sys.stderr) doctests = [ "basic_api.txt", ] doctest_options = doctest.ELLIPSIS #doctest_options |= doctest.REPORT_ONLY_FIRST_FAILURE def setup(): """Prepare test environment Sets HOME and creates a new process group so we can clean up easily later. """ os.environ['HOME'] = tempfile.mkdtemp(prefix='datastore-test') if 'PYTHONPATH' in os.environ : python_path = os.environ.get('PYTHONPATH').split(':') else : python_path = [] # Run tests on sources instead of on installed files. python_path = [os.path.abspath('../src')] + python_path os.environ['PYTHONPATH'] = ':'.join(python_path) os.environ['PATH'] = os.path.abspath('../bin')+':'+os.environ['PATH'] os.setpgid(0, 0) # prevent suicide in cleanup() signal.signal(signal.SIGTERM, signal.SIG_IGN) def cleanup(): """Clean up test environment. Kills all children and removes home directory. """ os.kill(0, signal.SIGTERM) shutil.rmtree(os.environ['HOME']) def test_suite(tests=None): global doctests suite = unittest.TestSuite() if tests: doctests = tests for dt in doctests: suite.addTest(doctest.DocFileSuite(dt, optionflags=doctest_options)) if len(sys.argv) <= 1: tests = os.listdir(os.curdir) tests = [n[:-3] for n in tests if n.startswith('test') and n.endswith('.py')] for test in tests: m = __import__(test) if hasattr(m, 'test_suite'): suite.addTest(m.test_suite()) return suite def run_tests(tests): runner = unittest.TextTestRunner(verbosity=1) suite = test_suite(tests) runner.run(suite) def main(my_name, arguments): if "--have-dbus" not in arguments: setup() try: pipe = subprocess.Popen(['dbus-launch', os.path.abspath(my_name), '--have-dbus']+arguments, cwd=os.environ['HOME']) return pipe.wait() finally: cleanup() else: run_tests(arguments[1:]) return 0 if __name__ == "__main__": sys.exit(main(sys.argv[0], sys.argv[1:]))