~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/TestUtil.py

  • Committer: Jelmer Vernooij
  • Date: 2011-12-16 19:18:39 UTC
  • mto: This revision was merged to the branch mainline in revision 6391.
  • Revision ID: jelmer@samba.org-20111216191839-eg681lxqibi1qxu1
Fix remaining tests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2010 Canonical Ltd
 
1
# Copyright (C) 2005-2011 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
19
19
import sys
20
20
import logging
21
21
import unittest
 
22
import weakref
22
23
 
23
24
from bzrlib import pyutils
24
25
 
29
30
 
30
31
 
31
32
class LogCollector(logging.Handler):
 
33
 
32
34
    def __init__(self):
33
35
        logging.Handler.__init__(self)
34
36
        self.records=[]
 
37
 
35
38
    def emit(self, record):
36
39
        self.records.append(record.getMessage())
37
40
 
60
63
                visitor.visitSuite(test)
61
64
                visitTests(test, visitor)
62
65
            else:
63
 
                print "unvisitable non-unittest.TestCase element %r (%r)" % (test, test.__class__)
 
66
                print "unvisitable non-unittest.TestCase element %r (%r)" % (
 
67
                    test, test.__class__)
 
68
 
 
69
 
 
70
class FailedCollectionCase(unittest.TestCase):
 
71
    """Pseudo-test to run and report failure if given case was uncollected"""
 
72
 
 
73
    def __init__(self, case):
 
74
        super(FailedCollectionCase, self).__init__("fail_uncollected")
 
75
        # GZ 2011-09-16: Maybe catch errors from id() method as cases may be
 
76
        #                in a bit of a funny state by now.
 
77
        self._problem_case_id = case.id()
 
78
 
 
79
    def id(self):
 
80
        if self._problem_case_id[-1:] == ")":
 
81
            return self._problem_case_id[:-1] + ",uncollected)"
 
82
        return self._problem_case_id + "(uncollected)"
 
83
 
 
84
    def fail_uncollected(self):
 
85
        self.fail("Uncollected test case: " + self._problem_case_id)
64
86
 
65
87
 
66
88
class TestSuite(unittest.TestSuite):
79
101
        tests = list(self)
80
102
        tests.reverse()
81
103
        self._tests = []
 
104
        stored_count = 0
 
105
        count_stored_tests = getattr(result, "_count_stored_tests", int)
 
106
        from bzrlib.tests import selftest_debug_flags
 
107
        notify = "uncollected_cases" in selftest_debug_flags
82
108
        while tests:
83
109
            if result.shouldStop:
84
110
                self._tests = reversed(tests)
85
111
                break
86
 
            tests.pop().run(result)
 
112
            case = _run_and_collect_case(tests.pop(), result)()
 
113
            new_stored_count = count_stored_tests()
 
114
            if case is not None and isinstance(case, unittest.TestCase):
 
115
                if stored_count == new_stored_count and notify:
 
116
                    # Testcase didn't fail, but somehow is still alive
 
117
                    FailedCollectionCase(case).run(result)
 
118
                    # Adding a new failure so need to reupdate the count
 
119
                    new_stored_count = count_stored_tests()
 
120
                # GZ 2011-09-16: Previously zombied the case at this point by
 
121
                #                clearing the dict as fallback, skip for now.
 
122
            stored_count = new_stored_count
87
123
        return result
88
124
 
89
125
 
 
126
def _run_and_collect_case(case, res):
 
127
    """Run test case against result and use weakref to drop the refcount"""
 
128
    case.run(res)
 
129
    return weakref.ref(case)
 
130
 
 
131
 
90
132
class TestLoader(unittest.TestLoader):
91
133
    """Custom TestLoader to extend the stock python one."""
92
134
 
183
225
 
184
226
class TestVisitor(object):
185
227
    """A visitor for Tests"""
 
228
 
186
229
    def visitSuite(self, aTestSuite):
187
230
        pass
 
231
 
188
232
    def visitCase(self, aTestCase):
189
233
        pass