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

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -