Python Unit Test Dictionary Assert KeyError -
i wasn't sure how create python unittest check if dictionary returned keyerror. thought unit test call dictionary key this:
def test_dict_keyerror_should_appear(self): my_dict = {'hey': 'world'} self.assertraises(keyerror, my_dict['some_key'])
however, test error out keyerror instead of asserting keyerror occurred.
to solve used lambda
call dictionary key raise error.
def test_dict_keyerror_should_appear(self): my_dict = {'hey': 'world'} self.assertraises(keyerror, lambda: my_dict['some_key'])
Comments
Post a Comment