How to assert for exceptions with python library unittest

  python, python-unittest, try-catch, unit-testing

I have a dummy funtion to try exceptions:

def fun(n):
    try:
        if n <1:
            raise ValueError
        return 1
        except:
            pass

In my unit test I use:

import  unittest
class TestFibonnacci(unittest.TestCase):
    def test_values(self):
        self.assertEqual(fun(1),1)
        self.assertRaises(ValueError, fun(-1))

However I’m unable to get an answer I actually get:

An exception occurred
E….
ERROR: test_values (main.TestFibonnacci)

Traceback (most recent call last):
The traceback here

TypeError: ‘NoneType’ object is not callable

Ran 1 tests in 0.001s

What I’m doing wrong?

Source: Python Questions

LEAVE A COMMENT