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 |
`~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.
def tornado.testing.AsyncTestCase.__init__ | ( | self, | |
methodName = 'runTest' , |
|||
kwargs | |||
) |
Definition at line 197 of file testing.py.
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.
def tornado.testing.AsyncTestCase.get_new_ioloop | ( | self | ) |
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.setUp | ( | self | ) |
Reimplemented in tornado.test.web_test.XSRFTest, tornado.test.httpserver_test.IdleTimeoutTest, tornado.test.httpserver_test.KeepAliveTest, tornado.test.simple_httpclient_test.ResolveTimeoutTestCase, tornado.test.simple_httpclient_test.HostnameMappingTestCase, tornado.test.simple_httpclient_test.SimpleHTTPClientTestCase, tornado.test.httpserver_test.HTTPServerRawTest, tornado.test.ioloop_test.TestIOLoopAddCallback, tornado.testing.AsyncHTTPTestCase, tornado.test.tcpclient_test.ConnectorTest, tornado.test.netutil_test.TwistedResolverTest, tornado.test.testing_test.GenTest, tornado.test.netutil_test.CaresResolverTest, tornado.test.netutil_test.ThreadedResolverTest, tornado.test.curl_httpclient_test.CurlHTTPClientTestCase, tornado.test.netutil_test.BlockingResolverTest, and tornado.test.tcpclient_test.TCPClientTest.
Definition at line 211 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.tearDown | ( | self | ) |
Reimplemented in tornado.test.web_test.GetCurrentUserTest, tornado.test.web_test.UIMethodUIModuleTest, tornado.test.httpserver_test.IdleTimeoutTest, tornado.test.httpserver_test.KeepAliveTest, tornado.test.web_test.WSGISafeWebTest, tornado.testing.AsyncHTTPTestCase, tornado.test.httpserver_test.HTTPServerRawTest, tornado.test.tcpclient_test.ConnectorTest, tornado.test.testing_test.GenTest, tornado.test.netutil_test.ThreadedResolverTest, and tornado.test.tcpclient_test.TCPClientTest.
Definition at line 216 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.
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.