~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/TestUtil.py

  • Committer: Martin Packman
  • Date: 2012-01-05 09:50:04 UTC
  • mfrom: (6424 +trunk)
  • mto: This revision was merged to the branch mainline in revision 6426.
  • Revision ID: martin.packman@canonical.com-20120105095004-mia9xb7y0efmto0v
Merge bzr.dev to resolve conflicts in bzrlib.builtins

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
66
67
                    test, test.__class__)
67
68
 
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)
 
86
 
 
87
 
69
88
class TestSuite(unittest.TestSuite):
70
89
    """I am an extended TestSuite with a visitor interface.
71
90
    This is primarily to allow filtering of tests - and suites or
82
101
        tests = list(self)
83
102
        tests.reverse()
84
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
85
108
        while tests:
86
109
            if result.shouldStop:
87
110
                self._tests = reversed(tests)
88
111
                break
89
 
            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
90
123
        return result
91
124
 
92
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
 
93
132
class TestLoader(unittest.TestLoader):
94
133
    """Custom TestLoader to extend the stock python one."""
95
134