2

I have my own exceptions and i want to test farther fields in the ex other then the message. Reading this thread i tried the idea of using a context. I wrote this generic function

def test_runtime_error(test, exception_type, message, display_parameter, path, callable_obj, *args):
    pdb.set_trace()
    with test.assertRaises(exception_type) as cx:
        callable_obj(*args)
    ex = cx.exception

    test.assertEqual(ex.message,message)
    test.assertEqual(ex.display_parameter,display_parameter)
    test.assertEqual(ex.path,path)

The path and display_parameter are my own specific fields. I'm not inventing the wheel here, i took most of it from the source.

I'm using it like that

class ExceptionsTest(unittest.TestCase):
    def test_something(self):
         data = {"name" : "A"}
         obj = MyModel.objects.get(pk=1)
         test_runtime_error(self,CustomException, 'message', 'A', [], obj.create, data)

The arguments are passed correctly into the callable_obj. the function raises the expected exception. but right after the execution of callable_obj the function breaks and the exception is not fetched. BTW, when i ran the same code in the test it self it worked fine.

Whats wrong here ?

Community
  • 1
  • 1
haki
  • 9,389
  • 15
  • 62
  • 110

1 Answers1

0

The issue here appears to be this line:

pdb.set_trace()

If you leave it in, but don't have import pdb, the code below will fail with:

E
======================================================================
ERROR: testRaises (__main__.ExceptionTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "./except.py", line 22, in testRaises
    self.verifyComplexException(MyException, 'asdf', RaiseException, 'asdf')
  File "./except.py", line 14, in verifyComplexException
    pdb.set_trace()
NameError: global name 'pdb' is not defined

----------------------------------------------------------------------
Ran 1 test in 0.000s

FAILED (errors=1)

which matches your description. If you do add the import pdb line, it will drop into the debugger, which is a completely different behavior that cannot be confused for the exit with E or exit with F status, so it can't be that.

Here's a complete example based on this idea which works as intended (licensed under Apache 2.0; see my repo):

import unittest

class MyException(Exception):
    def __init__(self, message):
        self.message = message

def RaiseException(message):
    raise MyException(message)

class ExceptionTest(unittest.TestCase):
    def verifyComplexException(self, exception_class, message, callable, *args):
        with self.assertRaises(exception_class) as cm:
            callable(*args)

        exception = cm.exception
        self.assertEqual(exception.message, message)

    def testRaises(self):
        self.verifyComplexException(MyException, 'asdf', RaiseException, 'asdf')


if __name__ == '__main__':
    unittest.main()
Misha Brukman
  • 12,938
  • 4
  • 61
  • 78