[issue9868] test_locale leaves locale changed

Hirokazu Yamamoto report at bugs.python.org
Thu Sep 16 06:11:40 CEST 2010


Hirokazu Yamamoto <ocean-city at m2.ccsnet.ne.jp> added the comment:

This happens because in Lib/test/test_locale.py,
TestEnUSCollation#setUp raises exception after
BaseLocalizedTest.setUp(self) changed locale.
tearDown never be called when setUp failedsetUp failed.

It's easy to fix this as is, but I think more general
solution would be better.

How about the mechanism like this? It calls setUp() and
tearDown() in according to MRO, if setUp() fails somewhere
in the chain, calls tearDown() for the class whose setUp()
is successfully called. (Be care, you shouldn't call
parent's setUp() or tearDown() directly)

////////////////////
// mockup

import unittest

class TestCase2(unittest.TestCase):
    def setUp(self):
        klasses = []
        try:
            for klass in reversed(self.__class__.__mro__):
                func = getattr(klass, "setUp2", None)
                if func is not None:
                    func(self)
                    klasses.append(klass)
        except:
            for klass in reversed(klasses):
                func = getattr(klass, "tearDown2", None)
                if func is not None:
                    func(self)
            raise
    def tearDown(self):
        for klass in self.__class__.__mro__:
            func = getattr(klass, "tearDown2", None)
            if func is not None:
                func(self)

class A(TestCase2):
    def setUp2(self):
        print("A#setUp2")
    def tearDown2(self):
        print("A#tearDown2")

class B(A):
    def setUp2(self):
        print("B#setUp2")
        raise RuntimeError("B#setUp2 failed")
    def tearDown2(self):
        print("B#tearDown2")
    def test(self):
        pass

if __name__ == '__main__':
    unittest.main()

////////////////////////////////
// result

A#setUp2
B#setUp2
A#tearDown2
(snip)

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue9868>
_______________________________________


More information about the Python-bugs-list mailing list