I'm trying to figure out how to write a trial test case which asserts an exception is raised.
Currently I have 2 simple methods to test (success and failure). Each method returns a deferred which has already been either callback'd or errback'd. Testing the success method works fine. When testing the failure method I expect to be able to assert that an exception was raised (using assertRaises).
However the test case fails and I get:
twisted.trial.unittest.FailTest: ConnectionRefusedError not raised (<Deferred at 0x920e28c current result: <twisted.python.failure.Failure <class 'twisted.internet.error.ConnectionRefusedError'>>> returned)
The code is as follows:
from twisted.trial.unittest import TestCase from twisted.internet.defer import inlineCallbacks, succeed, fail from twisted.internet.error import ConnectionRefusedError class MyObject: def success(self): return succeed(True) def failure(self): return fail(ConnectionRefusedError()) class TestErrBack(TestCase): def setUp(self): self.o = MyObject() @inlineCallbacks def test_success(self): result = yield self.o.success() self.assertTrue(result) @inlineCallbacks def test_failure(self): # this test case is failing ! yield self.assertRaises(ConnectionRefusedError, self.o.failure)
Am I using the right approach in test_failure ? I can use try...catch around the call to self.o.failure, but I don't think this approach is as good as using assertRaises.