-2

I have made a class (derived from Exception):

class InvalidFilterException(Exception):
      #some code here
      super().__init__(message)

Now, when I run tests on this module, using unittest.Testcase , running

assertRaises(Exception, InvalidFilterException, arg1, arg2)

Gives an Error:

AssertionError: Exception not raised by InvalidFilterException

Can someone tell me what should I put in place of Exception as the first argument in assertRaisesso that this test passes? I also tried using InvalidFilterException, as the argument, but that failed too, with the same output.

Ayush Shridhar
  • 115
  • 4
  • 11

1 Answers1

0

In your case you are trying to InvalidFilterException because the second argument to assertRaises is callable that you want to test.

You need to use your exception like...

assertRaises(InvalidFilterException, do_something, arg1, arg2)

or

with assertRaises(InvalidFilterException):
    do_something(arg1, arg2)

do_something is your function or callable that needs to be tested.

viveksyngh
  • 777
  • 7
  • 15
  • What exactly will the `do_something()` function do? As far as I know, the test will pass if `InvalidFilterException` is raised when `do_something(arg1,arg2)` is run. – Ayush Shridhar Aug 10 '17 at 09:07
  • Yes .. so the function or block of code that you want to test will be do_something – viveksyngh Aug 10 '17 at 09:30
  • You can refer to this link for more information... https://stackoverflow.com/questions/129507/how-do-you-test-that-a-python-function-throws-an-exception?rq=1 – viveksyngh Aug 10 '17 at 09:56