~jml/testresources/tests-meaning-cleanup

« back to all changes in this revision

Viewing changes to lib/testresources/__init__.py

  • Committer: Jonathan Lange
  • Date: 2008-08-28 10:00:13 UTC
  • Revision ID: jml@canonical.com-20080828100013-awods2lykzj7h6bn
More refactoring

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
18
#
19
19
 
20
 
from copy import copy
 
20
from pyunit3k import iterate_tests
21
21
import unittest
22
22
 
23
23
 
26
26
    return testresources.tests.test_suite()
27
27
 
28
28
 
29
 
def iterate_tests(test_suite_or_case):
30
 
    """Iterate through all of the test cases in `test_suite_or_case`."""
31
 
    try:
32
 
        suite = iter(test_suite_or_case)
33
 
    except TypeError:
34
 
        yield test_suite_or_case
35
 
    else:
36
 
        for test in suite:
37
 
            for subtest in iterate_tests(test):
38
 
                yield subtest
 
29
def split_by_resources(tests):
 
30
    """Split a list of tests by whether or not they use test resources.
 
31
 
 
32
    :return: ([tests_that_dont], [tests_that_do])
 
33
    """
 
34
    # XXX: We could probably use itertools.groupby for this. Or set
 
35
    # difference.
 
36
    resource_users = []
 
37
    legacy = []
 
38
    for test in tests:
 
39
        resources = getattr(test, 'resources', None)
 
40
        if resources:
 
41
            resource_users.append(test)
 
42
        else:
 
43
            legacy.append(test)
 
44
    return legacy, resource_users
39
45
 
40
46
 
41
47
class OptimizingTestSuite(unittest.TestSuite):
104
110
        # build a mesh graph where a node is a test, and and the number of
105
111
        # resources to change to another test is the cost to travel straight
106
112
        # to that node.
107
 
        legacy = []
108
 
        graph = {}
109
 
        pending = []
110
 
        temp_pending = copy(self._tests)
111
 
        if len(temp_pending) == 0:
112
 
            return {}, []
113
 
        for test in temp_pending:
114
 
            if getattr(test, 'resources', None) is None:
115
 
                legacy.append(test)
116
 
                continue
117
 
            pending.append(test)
118
 
            graph[test] = {}
119
 
        while len(pending):
 
113
        legacy, pending = split_by_resources(self._tests)
 
114
        graph = dict((test, dict()) for test in pending)
 
115
        while pending:
120
116
            test = pending.pop()
121
117
            test_resources = set(test.resources)
122
118
            for othertest in pending:
123
119
                othertest_resources = set(othertest.resources)
124
 
                cost = len(test_resources.symmetric_difference(
125
 
                                othertest_resources))
 
120
                cost = len(
 
121
                    test_resources.symmetric_difference(othertest_resources))
126
122
                graph[test][othertest] = cost
127
123
                graph[othertest][test] = cost
128
124
        return graph, legacy