lundi 30 mai 2016

Why doesn't Pythons unittest.assertRaises() raise an error here?

Using Python 3.5, why does all tests below pass when run? Since, an Exception is not raised when div is called, how come that assertRaises() doesn't complain?

According to the documentation for assertRaises(): "or fails if no exception is raised".

Can someone help me out?

..
----------------------------------------------------------------------
Ran 2 tests in 0.002s




def div(self, x, y):
    if y == 0:
        raise Exception("Division by zero")
    return x / y

class MyTest(unittest.TestCase):

    def test1(self):
        with self.assertRaises(Exception) as cm:
            self.div(2, 1)

    def test2(self):
        self.assertRaises(Exception, div, 2, 1)

Aucun commentaire:

Enregistrer un commentaire