Public Member Functions | Public Attributes | Private Member Functions | Private Attributes
tornado.testing.AsyncTestCase Class Reference
Inheritance diagram for tornado.testing.AsyncTestCase:
Inheritance graph
[legend]

List of all members.

Public Member Functions

def __init__
def get_new_ioloop
def run
def setUp
def stop
def tearDown
def wait

Public Attributes

 io_loop

Private Member Functions

def __rethrow
def _handle_exception

Private Attributes

 __failure
 __running
 __stop_args
 __stopped
 __timeout

Detailed Description

`~unittest.TestCase` subclass for testing `.IOLoop`-based
asynchronous code.

The unittest framework is synchronous, so the test must be
complete by the time the test method returns.  This means that
asynchronous code cannot be used in quite the same way as usual.
To write test functions that use the same ``yield``-based patterns
used with the `tornado.gen` module, decorate your test methods
with `tornado.testing.gen_test` instead of
`tornado.gen.coroutine`.  This class also provides the `stop()`
and `wait()` methods for a more manual style of testing.  The test
method itself must call ``self.wait()``, and asynchronous
callbacks should call ``self.stop()`` to signal completion.

By default, a new `.IOLoop` is constructed for each test and is available
as ``self.io_loop``.  This `.IOLoop` should be used in the construction of
HTTP clients/servers, etc.  If the code being tested requires a
global `.IOLoop`, subclasses should override `get_new_ioloop` to return it.

The `.IOLoop`'s ``start`` and ``stop`` methods should not be
called directly.  Instead, use `self.stop <stop>` and `self.wait
<wait>`.  Arguments passed to ``self.stop`` are returned from
``self.wait``.  It is possible to have multiple ``wait``/``stop``
cycles in the same test.

Example::

    # This test uses coroutine style.
    class MyTestCase(AsyncTestCase):
        @tornado.testing.gen_test
        def test_http_fetch(self):
            client = AsyncHTTPClient(self.io_loop)
            response = yield client.fetch("http://www.tornadoweb.org")
            # Test contents of response
            self.assertIn("FriendFeed", response.body)

    # This test uses argument passing between self.stop and self.wait.
    class MyTestCase2(AsyncTestCase):
        def test_http_fetch(self):
            client = AsyncHTTPClient(self.io_loop)
            client.fetch("http://www.tornadoweb.org/", self.stop)
            response = self.wait()
            # Test contents of response
            self.assertIn("FriendFeed", response.body)

    # This test uses an explicit callback-based style.
    class MyTestCase3(AsyncTestCase):
        def test_http_fetch(self):
            client = AsyncHTTPClient(self.io_loop)
            client.fetch("http://www.tornadoweb.org/", self.handle_fetch)
            self.wait()

        def handle_fetch(self, response):
            # Test contents of response (failures and exceptions here
            # will cause self.wait() to throw an exception and end the
            # test).
            # Exceptions thrown here are magically propagated to
            # self.wait() in test_http_fetch() via stack_context.
            self.assertIn("FriendFeed", response.body)
            self.stop()

Definition at line 135 of file testing.py.


Constructor & Destructor Documentation

def tornado.testing.AsyncTestCase.__init__ (   self,
  methodName = 'runTest',
  kwargs 
)

Definition at line 197 of file testing.py.


Member Function Documentation

def tornado.testing.AsyncTestCase.__rethrow (   self) [private]

Definition at line 244 of file testing.py.

def tornado.testing.AsyncTestCase._handle_exception (   self,
  typ,
  value,
  tb 
) [private]

Definition at line 239 of file testing.py.

Creates a new `.IOLoop` for this test.  May be overridden in
subclasses for tests that require a specific `.IOLoop` (usually
the singleton `.IOLoop.instance()`).

Definition at line 232 of file testing.py.

def tornado.testing.AsyncTestCase.run (   self,
  result = None 
)

Definition at line 250 of file testing.py.

def tornado.testing.AsyncTestCase.stop (   self,
  _arg = None,
  kwargs 
)
Stops the `.IOLoop`, causing one pending (or future) call to `wait()`
to return.

Keyword arguments or a single positional argument passed to `stop()` are
saved and will be returned by `wait()`.

Definition at line 259 of file testing.py.

def tornado.testing.AsyncTestCase.wait (   self,
  condition = None,
  timeout = None 
)
Runs the `.IOLoop` until stop is called or timeout has passed.

In the event of a timeout, an exception will be thrown. The
default timeout is 5 seconds; it may be overridden with a
``timeout`` keyword argument or globally with the
``ASYNC_TEST_TIMEOUT`` environment variable.

If ``condition`` is not None, the `.IOLoop` will be restarted
after `stop()` until ``condition()`` returns true.

.. versionchanged:: 3.1
   Added the ``ASYNC_TEST_TIMEOUT`` environment variable.

Definition at line 273 of file testing.py.


Member Data Documentation

Definition at line 197 of file testing.py.

Definition at line 197 of file testing.py.

Definition at line 197 of file testing.py.

Definition at line 197 of file testing.py.

Definition at line 197 of file testing.py.

Definition at line 211 of file testing.py.


The documentation for this class was generated from the following file:


rosbridge_server
Author(s): Jonathan Mace
autogenerated on Thu Jun 6 2019 21:51:51