I'm trying to figure out how to correctly use assertRaises()
in Python unit testing. I have a ValueError
in my function that gets raised. However, when testing in my unit tests, I'm getting mixed results with two different ways of using `assertRaises().
When I write the test like this, it works:
import unittest
class MyTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
pass
def test_error(self):
with self.assertRaises(ValueError):
func(a)
However, if I write my test like below, it does not work even though the ValueError
gets raised:
def test_error(self):
self.assertRaises(ValueError, func(a))
Does anyone have any insight as to why one way would work and the other wouldn't?