def test_with_closed(self): def f(): with self.conn: pass self.conn.close() self.assertRaises(psycopg2.InterfaceError, f)