~launchpad-pqm/launchpad/devel

8687.15.18 by Karl Fogel
Add the copyright header block to files under lib/canonical/.
1
# Copyright 2009 Canonical Ltd.  This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
7913.6.1 by Jonathan Lange
Customize the test result object.
3
4
"""Support code for using a custom test result in test.py."""
5
6
__metaclass__ = type
7
__all__ = [
7913.6.18 by Jonathan Lange
Docstrings and class renames.
8
    'filter_tests',
9
    'patch_find_tests',
7913.6.1 by Jonathan Lange
Customize the test result object.
10
    ]
11
7913.6.10 by Jonathan Lange
Get MultiTestResult from testtools.
12
from unittest import TestSuite
14612.2.1 by William Grant
format-imports on lib/. So many imports.
13
10446.3.1 by Jonathan Lange
Upgrade to zope.testing 3.9.0 and subunit trunk r120.
14
from testtools import iterate_tests
15
from zope.testing.testrunner import find
7913.6.2 by Jonathan Lange
Make the test runner report using trial reporter.
16
17
7913.6.6 by Jonathan Lange
Add some options to test.py to lay foundations for parallelized test running.
18
def patch_find_tests(hook):
7913.6.18 by Jonathan Lange
Docstrings and class renames.
19
    """Add a post-processing hook to zope.testing.testrunner.find_tests.
20
21
    This is useful for things like filtering tests or listing tests.
22
23
    :param hook: A callable that takes the output of the real
24
        `testrunner.find_tests` and returns a thing with the same type and
25
        structure.
26
    """
9572.2.2 by Jonathan Lange
Update for new location of find_tests.
27
    real_find_tests = find.find_tests
7913.6.6 by Jonathan Lange
Add some options to test.py to lay foundations for parallelized test running.
28
    def find_tests(*args):
29
        return hook(real_find_tests(*args))
9572.2.2 by Jonathan Lange
Update for new location of find_tests.
30
    find.find_tests = find_tests
7913.6.6 by Jonathan Lange
Add some options to test.py to lay foundations for parallelized test running.
31
32
7913.6.7 by Jonathan Lange, Robert Collins
Run tests based on a list of tests in a file.
33
def filter_tests(list_name):
7913.6.18 by Jonathan Lange
Docstrings and class renames.
34
    """Create a hook for `patch_find_tests` that filters tests based on id.
35
36
    :param list_name: A filename that contains a newline-separated list of
37
        test ids, as generated by `list_tests`.
38
    :return: A callable that takes a result of `testrunner.find_tests` and
39
        returns only those tests with ids in the file 'list_name'.
40
    """
7913.6.7 by Jonathan Lange, Robert Collins
Run tests based on a list of tests in a file.
41
    def do_filter(tests_by_layer_name):
7913.6.14 by Robert Collins, Jonathan Lange
Stable sort order for tests, correct multitestresult monkey patching.
42
        tests = sorted(set(line.strip() for line in open(list_name, 'rb')))
7913.6.7 by Jonathan Lange, Robert Collins
Run tests based on a list of tests in a file.
43
        result = {}
44
        for layer_name, suite in tests_by_layer_name.iteritems():
45
            new_suite = TestSuite()
46
            for test in iterate_tests(suite):
47
                if test.id() in tests:
48
                    new_suite.addTest(test)
49
            if new_suite.countTestCases():
50
                result[layer_name] = new_suite
51
        return result
52
    return do_filter