/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/TestUtil.py

  • Committer: Martin Pool
  • Date: 2008-01-04 23:53:04 UTC
  • mto: (3170.2.1 1.1)
  • mto: This revision was merged to the branch mainline in revision 3185.
  • Revision ID: mbp@sourcefrog.net-20080104235304-ms9b1y63r710qv2i
PrepareĀ 1.1rc1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (c) 2004 Canonical Limited
 
1
# Copyright (C) 2004, 2005, 2006 Canonical Ltd
2
2
#       Author: Robert Collins <robert.collins@canonical.com>
3
3
#
4
4
# This program is free software; you can redistribute it and/or modify
20
20
import logging
21
21
import unittest
22
22
 
 
23
# Mark this python module as being part of the implementation
 
24
# of unittest: this gives us better tracebacks where the last
 
25
# shown frame is the test code, not our assertXYZ.
 
26
__unittest = 1
 
27
 
23
28
 
24
29
class LogCollector(logging.Handler):
25
30
    def __init__(self):
69
74
 
70
75
 
71
76
class TestLoader(unittest.TestLoader):
72
 
    """Custome TestLoader to set the right TestSuite class."""
 
77
    """Custom TestLoader to extend the stock python one."""
 
78
 
73
79
    suiteClass = TestSuite
74
80
 
 
81
    def loadTestsFromModuleNames(self, names):
 
82
        """use a custom means to load tests from modules.
 
83
 
 
84
        There is an undesirable glitch in the python TestLoader where a 
 
85
        import error is ignore. We think this can be solved by ensuring the 
 
86
        requested name is resolvable, if its not raising the original error.
 
87
        """
 
88
        result = self.suiteClass()
 
89
        for name in names:
 
90
            module = _load_module_by_name(name)
 
91
            result.addTests(self.loadTestsFromModule(module))
 
92
        return result
 
93
 
 
94
    def loadTestsFromModule(self, module):
 
95
        """Load tests from a module object.
 
96
 
 
97
        This extension of the python test loader looks for an attribute
 
98
        load_tests in the module object, and if not found falls back to the
 
99
        regular python loadTestsFromModule.
 
100
 
 
101
        If a load_tests attribute is found, it is called and the result is
 
102
        returned. 
 
103
 
 
104
        load_tests should be defined like so:
 
105
        >>> def load_tests(standard_tests, module, loader):
 
106
        >>>    pass
 
107
 
 
108
        standard_tests is the tests found by the stock TestLoader in the
 
109
        module, module and loader are the module and loader instances.
 
110
 
 
111
        For instance, to run every test twice, you might do:
 
112
        >>> def load_tests(standard_tests, module, loader):
 
113
        >>>     result = loader.suiteClass()
 
114
        >>>     for test in iter_suite_tests(standard_tests):
 
115
        >>>         result.addTests([test, test])
 
116
        >>>     return result
 
117
        """
 
118
        basic_tests = super(TestLoader, self).loadTestsFromModule(module)
 
119
        load_tests = getattr(module, "load_tests", None)
 
120
        if load_tests is not None:
 
121
            return load_tests(basic_tests, module, self)
 
122
        else:
 
123
            return basic_tests
 
124
 
 
125
 
 
126
def _load_module_by_name(mod_name):
 
127
    parts = mod_name.split('.')
 
128
    module = __import__(mod_name)
 
129
    del parts[0]
 
130
    # for historical reasons python returns the top-level module even though
 
131
    # it loads the submodule; we need to walk down to get the one we want.
 
132
    while parts:
 
133
        module = getattr(module, parts.pop(0))
 
134
    return module
 
135
 
 
136
 
75
137
class TestVisitor(object):
76
138
    """A visitor for Tests"""
77
139
    def visitSuite(self, aTestSuite):