[Twisted-Python] Writing Unittests for Twisted Applications

Bernhard Herzog bh at intevation.de
Tue Jan 7 10:09:34 EST 2003


I wrote:

[Mixin classes for tests that use reactors]
> The test base class is finally publically available inder GPL for now:
> http://www.intevation.de/cgi-bin/viewcvs-greater.cgi/*checkout*/GREAT-ERModel/test/reactormixins.py?rev=1.1&content-type=text/plain

Someone asked for a usage example in private mail. There are a few test
cases in the same CVS repository that use the mix-ins, but more
illustrative is probably a very simple example like this:


import unittest

from reactormixins import ReactorMixin

class Test(unittest.TestCase, ReactorMixin):

    def setUp(self):
        """Extend inherited method to set later_has_been_called to 0"""
        self.later_has_been_called = 0
        ReactorMixin.setUp(self)

    def test(self):
        """Simple test to show how to use ReactorMixin"""
        # scheduler self.later to be run after 0.1 seconds
        self.callLater(0.1, self.later)

        # run the reactor with a 10s time out
        self.run_reactor(10)

        self.assert_(self.later_has_been_called)

    def later(self):
        """Record that later has been called and stop the reactor"""
        self.later_has_been_called = 1
        self.stop_reactor()

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


This example doesn't do any networking but should give an idea of how to
use the mixin classes.

The networking support in the mixins is currently limited to listening
because that all I needed so far. My testcases start sub-processes that
connect to the reactor in the test case.

   Bernhard

-- 
Intevation GmbH                                 http://intevation.de/
Sketch                                 http://sketch.sourceforge.net/
MapIt!                                           http://www.mapit.de/




More information about the Twisted-Python mailing list