~launchpad-results/launchpad-results/trunk

36 by Marc Tardif
Added support for testrepository and testing documentation.
1
# Copyright 2010-2011 Canonical Ltd.  This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
3
4
"""Support code for using a custom test result in test.py."""
5
6
__metaclass__ = type
7
8
__all__ = [
9
    "filter_tests",
10
    "patch_find_tests",
11
    ]
12
13
from unittest import TestSuite
14
15
from zope.testing.testrunner import find
16
17
18
def patch_find_tests(hook):
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
    """
27
    real_find_tests = find.find_tests
28
29
    def find_tests(*args):
30
        return hook(real_find_tests(*args))
31
32
    find.find_tests = find_tests
33
34
35
def filter_tests(list_name):
36
    """Create a hook for `patch_find_tests` that filters tests based on id.
37
38
    :param list_name: A filename that contains a newline-separated list of
39
        test ids, as generated by `list_tests`.
40
    :return: A callable that takes a result of `testrunner.find_tests` and
41
        returns only those tests with ids in the file `list_name`.
42
    """
43
    def do_filter(tests_by_layer_name):
41 by Marc Tardif
Fixed Makefile for syncdb target.
44
        from testtools import iterate_tests
45
36 by Marc Tardif
Added support for testrepository and testing documentation.
46
        tests = sorted(set(line.strip() for line in open(list_name, "rb")))
47
        result = {}
48
        for layer_name, suite in tests_by_layer_name.iteritems():
49
            new_suite = TestSuite()
50
            for test in iterate_tests(suite):
51
                if test.id() in tests:
52
                    new_suite.addTest(test)
53
            if new_suite.countTestCases():
54
                result[layer_name] = new_suite
55
        return result
56
    return do_filter