[Twisted-Python] Stopping Reactor in UnitTest Callback

Jonathan Lange jml at mumak.net
Sat Jan 12 09:26:04 EST 2013

On Sat, Jan 12, 2013 at 2:00 PM, Fabian Rothfuchs
<fabian.rothfuchs at googlemail.com> wrote:
> I'm in the dilemma of using both - Django and Twisted.
> My goal is to have Django initialising the test (python manage.py test
> <appname>), instead of trial, as it will setup the whole test environment
> (I.e. Creates a db copy, allows per-model testing, follows certain
> conventions, etc).
> As to that I'm trying to find a way how to start/stop the reactor from the
> test, as trial is not executed.
> Do you have an idea or an approach how this could be realised?

One thing that works for us is to have our Twisted-using tests
subclass testtools.TestCase[1], and declare that they are asynchronous
Twisted tests.


    class TestTwistedAPI(testtools.TestCase):
        run_tests_with = AsynchronousDeferredRunTest
        def test_foo(self):
            d = foo.some_api()
            d.addCallback(self.assertEqual, "bar")
            return d

Of course, not inheriting from Django's TestCase means you lose a lot
of its implicit set-up, but most of that can be called from public
APIs. We have a very rough project that does some of the

You could probably use twisted.trial.unittest.TestCase instead of


[1] http://pypi.python.org/pypi/testtools
[2] http://pypi.python.org/pypi/djangofixture

More information about the Twisted-Python mailing list