[Twisted-Python] testing my application

Jp Calderone exarkun at divmod.com
Thu Oct 20 09:30:49 EDT 2005

On Thu, 20 Oct 2005 15:19:15 +0200, Marcin Kasperski <marcin.kasperski at softax.com.pl> wrote:
>Dnia czwartek, 20 października 2005 13:16, Stefano Canepa
>> Hi all,
>> 	I am looking for a simple way to write test cases for my
>> project. What is the right way to be as standard as possible?
>> I know the application works becouse I developed a client and
>> the server speaking the same protocol and used lots of print
>> to have output and a sniffer to see network trafic but I
>> understand this is not the right way.
>Hmm, I have the similar problem. I have been using python
>unittest to test my application components but recently I
>started to write classess which return deferreds and I am not
>sure how to test them.
>First I tried to use standard unittest and install callbacks
>which call self.fail or self.succeed - but it seems that the
>errors are just ignored.
>Then I found on this list note about deferredResult and tried
>using it (keeping normal unittest) but seems the application
>just hangs here (for the simple reason I guess, no reactor is
>Then I found about twisted.trial.unittest. But when I replaced
>import unittest with from twisted.trial import unittest, I found
>that there is no 'main' function in this module.
>What should I do? Go back to normal unittest but start some
>reactor before unittest.main() ?
>Does there anywhere exist any complete ***standalone*** test
>example (run for itself instead of being run within whole
>twisted test framework)?

exarkun at boson:~$ cat > test_foo.py
from twisted.trial import unittest

from twisted.internet import defer

def myTestableFunction(x):
    return defer.succeed(x)

def myBrokenFunction(y):
    return defer.fail(ZeroDivisionError("Math is hard"))

def MyUnitTests(unittest.TestCase):
    def testTestable(self):
        return myTestableFunction(10).addCallback(self.assertEquals, 10)
    def testBroken(self):
        return self.assertFailure(myBrokenFunction("foo"), ZeroDivisionError)
exarkun at boson:~$ trial test_foo.py 
Running 2 tests.
      testBroken ...                          [OK]
      testTestable ...                        [OK]

Ran 2 tests in 0.033s

PASSED (successes=2)
exarkun at boson:~$ 

Hope this helps,


More information about the Twisted-Python mailing list