This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author ocean-city
Recipients ocean-city
Date 2010-09-16.04:11:39
SpamBayes Score 0.0
Marked as misclassified No
Message-id <1284610300.8.0.648288785811.issue9868@psf.upfronthosting.co.za>
In-reply-to
Content
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)
History
Date User Action Args
2010-09-16 04:11:40ocean-citysetrecipients: + ocean-city
2010-09-16 04:11:40ocean-citysetmessageid: <1284610300.8.0.648288785811.issue9868@psf.upfronthosting.co.za>
2010-09-16 04:11:39ocean-citylinkissue9868 messages
2010-09-16 04:11:39ocean-citycreate