assertRaises() will call the function for you and assertRaises() that this call throws an exception:
records = [1, 2] connector = DataConnector("BadConnection") self.assertRaises(KeyError, connector.generate_data, self.table, records)
Alternatively, use assertRaises() as the context manager:
with self.assertRaises(KeyError) as raises: DataConnector("BadConnection").generate_data(self.table, records)
which has the added benefit that the context manager then allows you to access the raised exception:
self.assertEqual(raises.exception.message, "BadConnection")
Martijn pieters
source share