old_contents: from tornado.testing import AsyncHTTPTestCase, gen_test
  from tornado.web import Application
  from tornado.websocket import WebSocketHandler, websocket_connect
  
  
  class EchoHandler(WebSocketHandler):
      def on_message(self, message):
          self.write_message(message, isinstance(message, bytes))
  
  
  class WebSocketTest(AsyncHTTPTestCase):
      def get_app(self):
          return Application([
              ('/echo', EchoHandler),
          ])
  
      @gen_test
      def test_websocket_gen(self):
          ws = yield websocket_connect(
              'ws://localhost:%d/echo' % self.get_http_port(),
              io_loop=self.io_loop)
          ws.write_message('hello')
          response = yield ws.read_message()
          self.assertEqual(response, 'hello')
  
      def test_websocket_callbacks(self):
          websocket_connect(
              'ws://localhost:%d/echo' % self.get_http_port(),
              io_loop=self.io_loop, callback=self.stop)
          ws = self.wait().result()
          ws.write_message('hello')
          ws.read_message(self.stop)
          response = self.wait().result()
          self.assertEqual(response, 'hello')
  
new_contents: from tornado.testing import AsyncHTTPTestCase, gen_test
  from tornado.web import Application
  from tornado.websocket import WebSocketHandler, websocket_connect
  
  
  class EchoHandler(WebSocketHandler):
      def on_message(self, message):
          self.write_message(message, isinstance(message, bytes))
  
  
  class WebSocketTest(AsyncHTTPTestCase):
      def get_app(self):
          return Application([
              ('/echo', EchoHandler),
          ])
  
      @gen_test
      def test_websocket_gen(self):
          ws = yield websocket_connect(
              'ws://localhost:%d/echo' % self.get_http_port(),
              io_loop=self.io_loop)
          ws.write_message('hello')
          response = yield ws.read_message()
          self.assertEqual(response, 'hello')
  
      def test_websocket_callbacks(self):
          websocket_connect(
              'ws://localhost:%d/echo' % self.get_http_port(),
              io_loop=self.io_loop, callback=self.stop)
          ws = self.wait().result()
          ws.write_message('hello')
          ws.read_message(self.stop)
          response = self.wait().result()
          self.assertEqual(response, 'hello')
      
      @gen_test
      def test_websocket_fail(self):
          try:
              ws = yield websocket_connect(
                  'ws://localhost:%d/no_websock' % self.get_http_port(),
                  io_loop=self.io_loop)
          except:
              pass
          else:
              self.fail('Should\'ve caught an Exception')
  
subject: test for websocket_connect failure