我正在尝试编写一个单元测试来验证 aKeyError
是在将坏键传递给字典时创建的。
引发异常的代码:
connections = SettingsManager().get_connections()
try:
connection = connections[self.conn_name]
except Exception:
self.log.error("Connection %s does not exist, exiting." % conn_name)
self.log.error(sys.exc_info()[0])
raise
我已经查看并找到KeyError
了使用 lambda 的测试,但我运气不佳。这是我到目前为止的测试,但它与实际的KeyError
.
def test_bad_connection(self):
#Testing to see if a non existant connection will fail gracefully.
records = [1, 2]
given_result = DataConnector("BadConnection").generate_data(self.table, records)
expected_result = "BadConnection"
self.assertRaises(KeyError, given_result[:1])