1 # Copyright (c) 2009 Jonathan M. Lange. See LICENSE for details.
3 """Test suites and related things."""
20 class ConcurrentTestSuite(unittest.TestSuite):
21 """A TestSuite whose run() calls out to a concurrency strategy."""
23 def __init__(self, suite, make_tests):
24 """Create a ConcurrentTestSuite to execute suite.
26 :param suite: A suite to run concurrently.
27 :param make_tests: A helper function to split the tests in the
28 ConcurrentTestSuite into some number of concurrently executing
29 sub-suites. make_tests must take a suite, and return an iterable
30 of TestCase-like object, each of which must have a run(result)
33 super(ConcurrentTestSuite, self).__init__([suite])
34 self.make_tests = make_tests
36 def run(self, result):
37 """Run the tests concurrently.
39 This calls out to the provided make_tests helper, and then serialises
40 the results so that result only sees activity from one TestCase at
43 ConcurrentTestSuite provides no special mechanism to stop the tests
44 returned by make_tests, it is up to the make_tests to honour the
45 shouldStop attribute on the result object they are run with, which will
46 be set if an exception is raised in the thread which
47 ConcurrentTestSuite.run is called in.
49 tests = self.make_tests(self)
53 result_semaphore = threading.Semaphore(1)
55 process_result = testtools.ThreadsafeForwardingResult(result,
57 reader_thread = threading.Thread(
58 target=self._run_test, args=(test, process_result, queue))
59 threads[test] = reader_thread, process_result
62 finished_test = queue.get()
63 threads[finished_test][0].join()
64 del threads[finished_test]
66 for thread, process_result in threads.values():
70 def _run_test(self, test, process_result, queue):
72 test.run(process_result)