[issue5538] tearDown in unittest should be executed regardless of result in setUp

Jean-Paul Calderone report at bugs.python.org
Mon Mar 23 12:41:25 CET 2009


Jean-Paul Calderone <exarkun at divmod.com> added the comment:

Here's one:

class ReactorShutdownInteraction(unittest.TestCase):
    """Test reactor shutdown interaction"""

    def setUp(self):
        """Start a UDP port"""
        self.server = Server()
        self.port = reactor.listenUDP(0, self.server, interface='127.0.0.1')

    def tearDown(self):
        """Stop the UDP port"""
        return self.port.stopListening()


Perhaps a feature like trial's `addCleanup´ should be added.  This lets
you deal with cleanup in a somewhat simpler way.  For example, rewriting
the above:

class ReactorShutdownInteraction(unittest.TestCase):
    """Test reactor shutdown interaction"""

    def setUp(self):
        """Start a UDP port"""
        self.server = Server()
        self.port = reactor.listenUDP(0, self.server, interface='127.0.0.1')
        # Stop the UDP port after the test completes.
        self.addCleanup(self.port.stopListening)

----------
nosy: +exarkun

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


More information about the Python-bugs-list mailing list