title: Testing for KeyError
body: <p>I'm trying to write a unit test that verifies a <code>KeyError</code> is created when a bad key is passed to a dictionary.</p>
  
  <p>The code that raises the exception:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>I've looked and found <code>KeyError</code> tests using lambda, but I've not had much luck.  Here is the test I have thus far, but it errors with the actual <code>KeyError</code>.</p>
  
  <pre><code>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])
  </code></pre>