testing.py 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  1. """Support classes for automated testing.
  2. * `AsyncTestCase` and `AsyncHTTPTestCase`: Subclasses of unittest.TestCase
  3. with additional support for testing asynchronous (`.IOLoop`-based) code.
  4. * `ExpectLog`: Make test logs less spammy.
  5. * `main()`: A simple test runner (wrapper around unittest.main()) with support
  6. for the tornado.autoreload module to rerun the tests when code changes.
  7. """
  8. import asyncio
  9. from collections.abc import Generator
  10. import functools
  11. import inspect
  12. import logging
  13. import os
  14. import re
  15. import signal
  16. import socket
  17. import sys
  18. import unittest
  19. import warnings
  20. from tornado import gen
  21. from tornado.httpclient import AsyncHTTPClient, HTTPResponse
  22. from tornado.httpserver import HTTPServer
  23. from tornado.ioloop import IOLoop, TimeoutError
  24. from tornado import netutil
  25. from tornado.platform.asyncio import AsyncIOMainLoop
  26. from tornado.process import Subprocess
  27. from tornado.log import app_log
  28. from tornado.util import raise_exc_info, basestring_type
  29. from tornado.web import Application
  30. import typing
  31. from typing import Tuple, Any, Callable, Type, Dict, Union, Optional, Coroutine
  32. from types import TracebackType
  33. if typing.TYPE_CHECKING:
  34. _ExcInfoTuple = Tuple[
  35. Optional[Type[BaseException]], Optional[BaseException], Optional[TracebackType]
  36. ]
  37. _NON_OWNED_IOLOOPS = AsyncIOMainLoop
  38. def bind_unused_port(
  39. reuse_port: bool = False, address: str = "127.0.0.1"
  40. ) -> Tuple[socket.socket, int]:
  41. """Binds a server socket to an available port on localhost.
  42. Returns a tuple (socket, port).
  43. .. versionchanged:: 4.4
  44. Always binds to ``127.0.0.1`` without resolving the name
  45. ``localhost``.
  46. .. versionchanged:: 6.2
  47. Added optional ``address`` argument to
  48. override the default "127.0.0.1".
  49. """
  50. sock = netutil.bind_sockets(
  51. 0, address, family=socket.AF_INET, reuse_port=reuse_port
  52. )[0]
  53. port = sock.getsockname()[1]
  54. return sock, port
  55. def get_async_test_timeout() -> float:
  56. """Get the global timeout setting for async tests.
  57. Returns a float, the timeout in seconds.
  58. .. versionadded:: 3.1
  59. """
  60. env = os.environ.get("ASYNC_TEST_TIMEOUT")
  61. if env is not None:
  62. try:
  63. return float(env)
  64. except ValueError:
  65. pass
  66. return 5
  67. class AsyncTestCase(unittest.TestCase):
  68. """`~unittest.TestCase` subclass for testing `.IOLoop`-based
  69. asynchronous code.
  70. The unittest framework is synchronous, so the test must be
  71. complete by the time the test method returns. This means that
  72. asynchronous code cannot be used in quite the same way as usual
  73. and must be adapted to fit. To write your tests with coroutines,
  74. decorate your test methods with `tornado.testing.gen_test` instead
  75. of `tornado.gen.coroutine`.
  76. This class also provides the (deprecated) `stop()` and `wait()`
  77. methods for a more manual style of testing. The test method itself
  78. must call ``self.wait()``, and asynchronous callbacks should call
  79. ``self.stop()`` to signal completion.
  80. By default, a new `.IOLoop` is constructed for each test and is available
  81. as ``self.io_loop``. If the code being tested requires a
  82. reused global `.IOLoop`, subclasses should override `get_new_ioloop` to return it,
  83. although this is deprecated as of Tornado 6.3.
  84. The `.IOLoop`'s ``start`` and ``stop`` methods should not be
  85. called directly. Instead, use `self.stop <stop>` and `self.wait
  86. <wait>`. Arguments passed to ``self.stop`` are returned from
  87. ``self.wait``. It is possible to have multiple ``wait``/``stop``
  88. cycles in the same test.
  89. Example::
  90. # This test uses coroutine style.
  91. class MyTestCase(AsyncTestCase):
  92. @tornado.testing.gen_test
  93. def test_http_fetch(self):
  94. client = AsyncHTTPClient()
  95. response = yield client.fetch("http://www.tornadoweb.org")
  96. # Test contents of response
  97. self.assertIn("FriendFeed", response.body)
  98. # This test uses argument passing between self.stop and self.wait.
  99. class MyTestCase2(AsyncTestCase):
  100. def test_http_fetch(self):
  101. client = AsyncHTTPClient()
  102. client.fetch("http://www.tornadoweb.org/", self.stop)
  103. response = self.wait()
  104. # Test contents of response
  105. self.assertIn("FriendFeed", response.body)
  106. """
  107. def __init__(self, methodName: str = "runTest") -> None:
  108. super().__init__(methodName)
  109. self.__stopped = False
  110. self.__running = False
  111. self.__failure = None # type: Optional[_ExcInfoTuple]
  112. self.__stop_args = None # type: Any
  113. self.__timeout = None # type: Optional[object]
  114. # Not used in this class itself, but used by @gen_test
  115. self._test_generator = None # type: Optional[Union[Generator, Coroutine]]
  116. def setUp(self) -> None:
  117. py_ver = sys.version_info
  118. if ((3, 10, 0) <= py_ver < (3, 10, 9)) or ((3, 11, 0) <= py_ver <= (3, 11, 1)):
  119. # Early releases in the Python 3.10 and 3.1 series had deprecation
  120. # warnings that were later reverted; we must suppress them here.
  121. setup_with_context_manager(self, warnings.catch_warnings())
  122. warnings.filterwarnings(
  123. "ignore",
  124. message="There is no current event loop",
  125. category=DeprecationWarning,
  126. module=r"tornado\..*",
  127. )
  128. super().setUp()
  129. if type(self).get_new_ioloop is not AsyncTestCase.get_new_ioloop:
  130. warnings.warn("get_new_ioloop is deprecated", DeprecationWarning)
  131. self.io_loop = self.get_new_ioloop()
  132. asyncio.set_event_loop(self.io_loop.asyncio_loop) # type: ignore[attr-defined]
  133. def tearDown(self) -> None:
  134. # Native coroutines tend to produce warnings if they're not
  135. # allowed to run to completion. It's difficult to ensure that
  136. # this always happens in tests, so cancel any tasks that are
  137. # still pending by the time we get here.
  138. asyncio_loop = self.io_loop.asyncio_loop # type: ignore
  139. tasks = asyncio.all_tasks(asyncio_loop)
  140. # Tasks that are done may still appear here and may contain
  141. # non-cancellation exceptions, so filter them out.
  142. tasks = [t for t in tasks if not t.done()] # type: ignore
  143. for t in tasks:
  144. t.cancel()
  145. # Allow the tasks to run and finalize themselves (which means
  146. # raising a CancelledError inside the coroutine). This may
  147. # just transform the "task was destroyed but it is pending"
  148. # warning into a "uncaught CancelledError" warning, but
  149. # catching CancelledErrors in coroutines that may leak is
  150. # simpler than ensuring that no coroutines leak.
  151. if tasks:
  152. done, pending = self.io_loop.run_sync(lambda: asyncio.wait(tasks))
  153. assert not pending
  154. # If any task failed with anything but a CancelledError, raise it.
  155. for f in done:
  156. try:
  157. f.result()
  158. except asyncio.CancelledError:
  159. pass
  160. # Clean up Subprocess, so it can be used again with a new ioloop.
  161. Subprocess.uninitialize()
  162. asyncio.set_event_loop(None)
  163. if not isinstance(self.io_loop, _NON_OWNED_IOLOOPS):
  164. # Try to clean up any file descriptors left open in the ioloop.
  165. # This avoids leaks, especially when tests are run repeatedly
  166. # in the same process with autoreload (because curl does not
  167. # set FD_CLOEXEC on its file descriptors)
  168. self.io_loop.close(all_fds=True)
  169. super().tearDown()
  170. # In case an exception escaped or the StackContext caught an exception
  171. # when there wasn't a wait() to re-raise it, do so here.
  172. # This is our last chance to raise an exception in a way that the
  173. # unittest machinery understands.
  174. self.__rethrow()
  175. def get_new_ioloop(self) -> IOLoop:
  176. """Returns the `.IOLoop` to use for this test.
  177. By default, a new `.IOLoop` is created for each test.
  178. Subclasses may override this method to return
  179. `.IOLoop.current()` if it is not appropriate to use a new
  180. `.IOLoop` in each tests (for example, if there are global
  181. singletons using the default `.IOLoop`) or if a per-test event
  182. loop is being provided by another system (such as
  183. ``pytest-asyncio``).
  184. .. deprecated:: 6.3
  185. This method will be removed in Tornado 7.0.
  186. """
  187. return IOLoop(make_current=False)
  188. def _handle_exception(
  189. self, typ: Type[Exception], value: Exception, tb: TracebackType
  190. ) -> bool:
  191. if self.__failure is None:
  192. self.__failure = (typ, value, tb)
  193. else:
  194. app_log.error(
  195. "multiple unhandled exceptions in test", exc_info=(typ, value, tb)
  196. )
  197. self.stop()
  198. return True
  199. def __rethrow(self) -> None:
  200. if self.__failure is not None:
  201. failure = self.__failure
  202. self.__failure = None
  203. raise_exc_info(failure)
  204. def run(
  205. self, result: Optional[unittest.TestResult] = None
  206. ) -> Optional[unittest.TestResult]:
  207. ret = super().run(result)
  208. # As a last resort, if an exception escaped super.run() and wasn't
  209. # re-raised in tearDown, raise it here. This will cause the
  210. # unittest run to fail messily, but that's better than silently
  211. # ignoring an error.
  212. self.__rethrow()
  213. return ret
  214. def _callTestMethod(self, method: Callable) -> None:
  215. """Run the given test method, raising an error if it returns non-None.
  216. Failure to decorate asynchronous test methods with ``@gen_test`` can lead to tests
  217. incorrectly passing.
  218. Remove this override when Python 3.10 support is dropped. This check (in the form of a
  219. DeprecationWarning) became a part of the standard library in 3.11.
  220. Note that ``_callTestMethod`` is not documented as a public interface. However, it is
  221. present in all supported versions of Python (3.8+), and if it goes away in the future that's
  222. OK because we can just remove this override as noted above.
  223. """
  224. # Calling super()._callTestMethod would hide the return value, even in python 3.8-3.10
  225. # where the check isn't being done for us.
  226. result = method()
  227. if isinstance(result, Generator) or inspect.iscoroutine(result):
  228. raise TypeError(
  229. "Generator and coroutine test methods should be"
  230. " decorated with tornado.testing.gen_test"
  231. )
  232. elif result is not None:
  233. raise ValueError("Return value from test method ignored: %r" % result)
  234. def stop(self, _arg: Any = None, **kwargs: Any) -> None:
  235. """Stops the `.IOLoop`, causing one pending (or future) call to `wait()`
  236. to return.
  237. Keyword arguments or a single positional argument passed to `stop()` are
  238. saved and will be returned by `wait()`.
  239. .. deprecated:: 5.1
  240. `stop` and `wait` are deprecated; use ``@gen_test`` instead.
  241. """
  242. assert _arg is None or not kwargs
  243. self.__stop_args = kwargs or _arg
  244. if self.__running:
  245. self.io_loop.stop()
  246. self.__running = False
  247. self.__stopped = True
  248. def wait(
  249. self,
  250. condition: Optional[Callable[..., bool]] = None,
  251. timeout: Optional[float] = None,
  252. ) -> Any:
  253. """Runs the `.IOLoop` until stop is called or timeout has passed.
  254. In the event of a timeout, an exception will be thrown. The
  255. default timeout is 5 seconds; it may be overridden with a
  256. ``timeout`` keyword argument or globally with the
  257. ``ASYNC_TEST_TIMEOUT`` environment variable.
  258. If ``condition`` is not ``None``, the `.IOLoop` will be restarted
  259. after `stop()` until ``condition()`` returns ``True``.
  260. .. versionchanged:: 3.1
  261. Added the ``ASYNC_TEST_TIMEOUT`` environment variable.
  262. .. deprecated:: 5.1
  263. `stop` and `wait` are deprecated; use ``@gen_test`` instead.
  264. """
  265. if timeout is None:
  266. timeout = get_async_test_timeout()
  267. if not self.__stopped:
  268. if timeout:
  269. def timeout_func() -> None:
  270. try:
  271. raise self.failureException(
  272. "Async operation timed out after %s seconds" % timeout
  273. )
  274. except Exception:
  275. self.__failure = sys.exc_info()
  276. self.stop()
  277. self.__timeout = self.io_loop.add_timeout(
  278. self.io_loop.time() + timeout, timeout_func
  279. )
  280. while True:
  281. self.__running = True
  282. self.io_loop.start()
  283. if self.__failure is not None or condition is None or condition():
  284. break
  285. if self.__timeout is not None:
  286. self.io_loop.remove_timeout(self.__timeout)
  287. self.__timeout = None
  288. assert self.__stopped
  289. self.__stopped = False
  290. self.__rethrow()
  291. result = self.__stop_args
  292. self.__stop_args = None
  293. return result
  294. class AsyncHTTPTestCase(AsyncTestCase):
  295. """A test case that starts up an HTTP server.
  296. Subclasses must override `get_app()`, which returns the
  297. `tornado.web.Application` (or other `.HTTPServer` callback) to be tested.
  298. Tests will typically use the provided ``self.http_client`` to fetch
  299. URLs from this server.
  300. Example, assuming the "Hello, world" example from the user guide is in
  301. ``hello.py``::
  302. import hello
  303. class TestHelloApp(AsyncHTTPTestCase):
  304. def get_app(self):
  305. return hello.make_app()
  306. def test_homepage(self):
  307. response = self.fetch('/')
  308. self.assertEqual(response.code, 200)
  309. self.assertEqual(response.body, 'Hello, world')
  310. That call to ``self.fetch()`` is equivalent to ::
  311. self.http_client.fetch(self.get_url('/'), self.stop)
  312. response = self.wait()
  313. which illustrates how AsyncTestCase can turn an asynchronous operation,
  314. like ``http_client.fetch()``, into a synchronous operation. If you need
  315. to do other asynchronous operations in tests, you'll probably need to use
  316. ``stop()`` and ``wait()`` yourself.
  317. """
  318. def setUp(self) -> None:
  319. super().setUp()
  320. sock, port = bind_unused_port()
  321. self.__port = port
  322. self.http_client = self.get_http_client()
  323. self._app = self.get_app()
  324. self.http_server = self.get_http_server()
  325. self.http_server.add_sockets([sock])
  326. def get_http_client(self) -> AsyncHTTPClient:
  327. return AsyncHTTPClient()
  328. def get_http_server(self) -> HTTPServer:
  329. return HTTPServer(self._app, **self.get_httpserver_options())
  330. def get_app(self) -> Application:
  331. """Should be overridden by subclasses to return a
  332. `tornado.web.Application` or other `.HTTPServer` callback.
  333. """
  334. raise NotImplementedError()
  335. def fetch(
  336. self, path: str, raise_error: bool = False, **kwargs: Any
  337. ) -> HTTPResponse:
  338. """Convenience method to synchronously fetch a URL.
  339. The given path will be appended to the local server's host and
  340. port. Any additional keyword arguments will be passed directly to
  341. `.AsyncHTTPClient.fetch` (and so could be used to pass
  342. ``method="POST"``, ``body="..."``, etc).
  343. If the path begins with http:// or https://, it will be treated as a
  344. full URL and will be fetched as-is.
  345. If ``raise_error`` is ``True``, a `tornado.httpclient.HTTPError` will
  346. be raised if the response code is not 200. This is the same behavior
  347. as the ``raise_error`` argument to `.AsyncHTTPClient.fetch`, but
  348. the default is ``False`` here (it's ``True`` in `.AsyncHTTPClient`)
  349. because tests often need to deal with non-200 response codes.
  350. .. versionchanged:: 5.0
  351. Added support for absolute URLs.
  352. .. versionchanged:: 5.1
  353. Added the ``raise_error`` argument.
  354. .. deprecated:: 5.1
  355. This method currently turns any exception into an
  356. `.HTTPResponse` with status code 599. In Tornado 6.0,
  357. errors other than `tornado.httpclient.HTTPError` will be
  358. passed through, and ``raise_error=False`` will only
  359. suppress errors that would be raised due to non-200
  360. response codes.
  361. """
  362. if path.lower().startswith(("http://", "https://")):
  363. url = path
  364. else:
  365. url = self.get_url(path)
  366. return self.io_loop.run_sync(
  367. lambda: self.http_client.fetch(url, raise_error=raise_error, **kwargs),
  368. timeout=get_async_test_timeout(),
  369. )
  370. def get_httpserver_options(self) -> Dict[str, Any]:
  371. """May be overridden by subclasses to return additional
  372. keyword arguments for the server.
  373. """
  374. return {}
  375. def get_http_port(self) -> int:
  376. """Returns the port used by the server.
  377. A new port is chosen for each test.
  378. """
  379. return self.__port
  380. def get_protocol(self) -> str:
  381. return "http"
  382. def get_url(self, path: str) -> str:
  383. """Returns an absolute url for the given path on the test server."""
  384. return f"{self.get_protocol()}://127.0.0.1:{self.get_http_port()}{path}"
  385. def tearDown(self) -> None:
  386. self.http_server.stop()
  387. self.io_loop.run_sync(
  388. self.http_server.close_all_connections, timeout=get_async_test_timeout()
  389. )
  390. self.http_client.close()
  391. del self.http_server
  392. del self._app
  393. super().tearDown()
  394. class AsyncHTTPSTestCase(AsyncHTTPTestCase):
  395. """A test case that starts an HTTPS server.
  396. Interface is generally the same as `AsyncHTTPTestCase`.
  397. """
  398. def get_http_client(self) -> AsyncHTTPClient:
  399. return AsyncHTTPClient(force_instance=True, defaults=dict(validate_cert=False))
  400. def get_httpserver_options(self) -> Dict[str, Any]:
  401. return dict(ssl_options=self.get_ssl_options())
  402. def get_ssl_options(self) -> Dict[str, Any]:
  403. """May be overridden by subclasses to select SSL options.
  404. By default includes a self-signed testing certificate.
  405. """
  406. return AsyncHTTPSTestCase.default_ssl_options()
  407. @staticmethod
  408. def default_ssl_options() -> Dict[str, Any]:
  409. # Testing keys were generated with:
  410. # openssl req -new -keyout tornado/test/test.key \
  411. # -out tornado/test/test.crt \
  412. # -nodes -days 3650 -x509 \
  413. # -subj "/CN=foo.example.com" -addext "subjectAltName = DNS:foo.example.com"
  414. module_dir = os.path.dirname(__file__)
  415. return dict(
  416. certfile=os.path.join(module_dir, "test", "test.crt"),
  417. keyfile=os.path.join(module_dir, "test", "test.key"),
  418. )
  419. def get_protocol(self) -> str:
  420. return "https"
  421. @typing.overload
  422. def gen_test(
  423. *, timeout: Optional[float] = None
  424. ) -> Callable[[Callable[..., Union[Generator, "Coroutine"]]], Callable[..., None]]:
  425. pass
  426. @typing.overload # noqa: F811
  427. def gen_test(func: Callable[..., Union[Generator, "Coroutine"]]) -> Callable[..., None]:
  428. pass
  429. def gen_test( # noqa: F811
  430. func: Optional[Callable[..., Union[Generator, "Coroutine"]]] = None,
  431. timeout: Optional[float] = None,
  432. ) -> Union[
  433. Callable[..., None],
  434. Callable[[Callable[..., Union[Generator, "Coroutine"]]], Callable[..., None]],
  435. ]:
  436. """Testing equivalent of ``@gen.coroutine``, to be applied to test methods.
  437. ``@gen.coroutine`` cannot be used on tests because the `.IOLoop` is not
  438. already running. ``@gen_test`` should be applied to test methods
  439. on subclasses of `AsyncTestCase`.
  440. Example::
  441. class MyTest(AsyncHTTPTestCase):
  442. @gen_test
  443. def test_something(self):
  444. response = yield self.http_client.fetch(self.get_url('/'))
  445. By default, ``@gen_test`` times out after 5 seconds. The timeout may be
  446. overridden globally with the ``ASYNC_TEST_TIMEOUT`` environment variable,
  447. or for each test with the ``timeout`` keyword argument::
  448. class MyTest(AsyncHTTPTestCase):
  449. @gen_test(timeout=10)
  450. def test_something_slow(self):
  451. response = yield self.http_client.fetch(self.get_url('/'))
  452. Note that ``@gen_test`` is incompatible with `AsyncTestCase.stop`,
  453. `AsyncTestCase.wait`, and `AsyncHTTPTestCase.fetch`. Use ``yield
  454. self.http_client.fetch(self.get_url())`` as shown above instead.
  455. .. versionadded:: 3.1
  456. The ``timeout`` argument and ``ASYNC_TEST_TIMEOUT`` environment
  457. variable.
  458. .. versionchanged:: 4.0
  459. The wrapper now passes along ``*args, **kwargs`` so it can be used
  460. on functions with arguments.
  461. """
  462. if timeout is None:
  463. timeout = get_async_test_timeout()
  464. def wrap(f: Callable[..., Union[Generator, "Coroutine"]]) -> Callable[..., None]:
  465. # Stack up several decorators to allow us to access the generator
  466. # object itself. In the innermost wrapper, we capture the generator
  467. # and save it in an attribute of self. Next, we run the wrapped
  468. # function through @gen.coroutine. Finally, the coroutine is
  469. # wrapped again to make it synchronous with run_sync.
  470. #
  471. # This is a good case study arguing for either some sort of
  472. # extensibility in the gen decorators or cancellation support.
  473. @functools.wraps(f)
  474. def pre_coroutine(self, *args, **kwargs):
  475. # type: (AsyncTestCase, *Any, **Any) -> Union[Generator, Coroutine]
  476. # Type comments used to avoid pypy3 bug.
  477. result = f(self, *args, **kwargs)
  478. if isinstance(result, Generator) or inspect.iscoroutine(result):
  479. self._test_generator = result
  480. else:
  481. self._test_generator = None
  482. return result
  483. if inspect.iscoroutinefunction(f):
  484. coro = pre_coroutine
  485. else:
  486. coro = gen.coroutine(pre_coroutine) # type: ignore[assignment]
  487. @functools.wraps(coro)
  488. def post_coroutine(self, *args, **kwargs):
  489. # type: (AsyncTestCase, *Any, **Any) -> None
  490. try:
  491. return self.io_loop.run_sync(
  492. functools.partial(coro, self, *args, **kwargs), timeout=timeout
  493. )
  494. except TimeoutError as e:
  495. # run_sync raises an error with an unhelpful traceback.
  496. # If the underlying generator is still running, we can throw the
  497. # exception back into it so the stack trace is replaced by the
  498. # point where the test is stopped. The only reason the generator
  499. # would not be running would be if it were cancelled, which means
  500. # a native coroutine, so we can rely on the cr_running attribute.
  501. if self._test_generator is not None and getattr(
  502. self._test_generator, "cr_running", True
  503. ):
  504. self._test_generator.throw(e)
  505. # In case the test contains an overly broad except
  506. # clause, we may get back here.
  507. # Coroutine was stopped or didn't raise a useful stack trace,
  508. # so re-raise the original exception which is better than nothing.
  509. raise
  510. return post_coroutine
  511. if func is not None:
  512. # Used like:
  513. # @gen_test
  514. # def f(self):
  515. # pass
  516. return wrap(func)
  517. else:
  518. # Used like @gen_test(timeout=10)
  519. return wrap
  520. # Without this attribute, nosetests will try to run gen_test as a test
  521. # anywhere it is imported.
  522. gen_test.__test__ = False # type: ignore
  523. class ExpectLog(logging.Filter):
  524. """Context manager to capture and suppress expected log output.
  525. Useful to make tests of error conditions less noisy, while still
  526. leaving unexpected log entries visible. *Not thread safe.*
  527. The attribute ``logged_stack`` is set to ``True`` if any exception
  528. stack trace was logged.
  529. Usage::
  530. with ExpectLog('tornado.application', "Uncaught exception"):
  531. error_response = self.fetch("/some_page")
  532. .. versionchanged:: 4.3
  533. Added the ``logged_stack`` attribute.
  534. """
  535. def __init__(
  536. self,
  537. logger: Union[logging.Logger, basestring_type],
  538. regex: str,
  539. required: bool = True,
  540. level: Optional[int] = None,
  541. ) -> None:
  542. """Constructs an ExpectLog context manager.
  543. :param logger: Logger object (or name of logger) to watch. Pass an
  544. empty string to watch the root logger.
  545. :param regex: Regular expression to match. Any log entries on the
  546. specified logger that match this regex will be suppressed.
  547. :param required: If true, an exception will be raised if the end of the
  548. ``with`` statement is reached without matching any log entries.
  549. :param level: A constant from the ``logging`` module indicating the
  550. expected log level. If this parameter is provided, only log messages
  551. at this level will be considered to match. Additionally, the
  552. supplied ``logger`` will have its level adjusted if necessary (for
  553. the duration of the ``ExpectLog`` to enable the expected message.
  554. .. versionchanged:: 6.1
  555. Added the ``level`` parameter.
  556. .. deprecated:: 6.3
  557. In Tornado 7.0, only ``WARNING`` and higher logging levels will be
  558. matched by default. To match ``INFO`` and lower levels, the ``level``
  559. argument must be used. This is changing to minimize differences
  560. between ``tornado.testing.main`` (which enables ``INFO`` logs by
  561. default) and most other test runners (including those in IDEs)
  562. which have ``INFO`` logs disabled by default.
  563. """
  564. if isinstance(logger, basestring_type):
  565. logger = logging.getLogger(logger)
  566. self.logger = logger
  567. self.regex = re.compile(regex)
  568. self.required = required
  569. # matched and deprecated_level_matched are a counter for the respective event.
  570. self.matched = 0
  571. self.deprecated_level_matched = 0
  572. self.logged_stack = False
  573. self.level = level
  574. self.orig_level = None # type: Optional[int]
  575. def filter(self, record: logging.LogRecord) -> bool:
  576. if record.exc_info:
  577. self.logged_stack = True
  578. message = record.getMessage()
  579. if self.regex.match(message):
  580. if self.level is None and record.levelno < logging.WARNING:
  581. # We're inside the logging machinery here so generating a DeprecationWarning
  582. # here won't be reported cleanly (if warnings-as-errors is enabled, the error
  583. # just gets swallowed by the logging module), and even if it were it would
  584. # have the wrong stack trace. Just remember this fact and report it in
  585. # __exit__ instead.
  586. self.deprecated_level_matched += 1
  587. if self.level is not None and record.levelno != self.level:
  588. app_log.warning(
  589. "Got expected log message %r at unexpected level (%s vs %s)"
  590. % (message, logging.getLevelName(self.level), record.levelname)
  591. )
  592. return True
  593. self.matched += 1
  594. return False
  595. return True
  596. def __enter__(self) -> "ExpectLog":
  597. if self.level is not None and self.level < self.logger.getEffectiveLevel():
  598. self.orig_level = self.logger.level
  599. self.logger.setLevel(self.level)
  600. self.logger.addFilter(self)
  601. return self
  602. def __exit__(
  603. self,
  604. typ: "Optional[Type[BaseException]]",
  605. value: Optional[BaseException],
  606. tb: Optional[TracebackType],
  607. ) -> None:
  608. if self.orig_level is not None:
  609. self.logger.setLevel(self.orig_level)
  610. self.logger.removeFilter(self)
  611. if not typ and self.required and not self.matched:
  612. raise Exception("did not get expected log message")
  613. if (
  614. not typ
  615. and self.required
  616. and (self.deprecated_level_matched >= self.matched)
  617. ):
  618. warnings.warn(
  619. "ExpectLog matched at INFO or below without level argument",
  620. DeprecationWarning,
  621. )
  622. # From https://nedbatchelder.com/blog/201508/using_context_managers_in_test_setup.html
  623. def setup_with_context_manager(testcase: unittest.TestCase, cm: Any) -> Any:
  624. """Use a context manager to setUp a test case.
  625. Example::
  626. def setUp(self):
  627. setup_with_context_manager(self, warnings.catch_warnings())
  628. warnings.filterwarnings("ignore", category=DeprecationWarning)
  629. # The catch_warnings context manager will be deactivated
  630. # automatically in tearDown.
  631. """
  632. val = cm.__enter__()
  633. testcase.addCleanup(cm.__exit__, None, None, None)
  634. return val
  635. def main(**kwargs: Any) -> None:
  636. """A simple test runner.
  637. This test runner is essentially equivalent to `unittest.main` from
  638. the standard library, but adds support for Tornado-style option
  639. parsing and log formatting. It is *not* necessary to use this
  640. `main` function to run tests using `AsyncTestCase`; these tests
  641. are self-contained and can run with any test runner.
  642. The easiest way to run a test is via the command line::
  643. python -m tornado.testing tornado.test.web_test
  644. See the standard library ``unittest`` module for ways in which
  645. tests can be specified.
  646. Projects with many tests may wish to define a test script like
  647. ``tornado/test/runtests.py``. This script should define a method
  648. ``all()`` which returns a test suite and then call
  649. `tornado.testing.main()`. Note that even when a test script is
  650. used, the ``all()`` test suite may be overridden by naming a
  651. single test on the command line::
  652. # Runs all tests
  653. python -m tornado.test.runtests
  654. # Runs one test
  655. python -m tornado.test.runtests tornado.test.web_test
  656. Additional keyword arguments passed through to ``unittest.main()``.
  657. For example, use ``tornado.testing.main(verbosity=2)``
  658. to show many test details as they are run.
  659. See http://docs.python.org/library/unittest.html#unittest.main
  660. for full argument list.
  661. .. versionchanged:: 5.0
  662. This function produces no output of its own; only that produced
  663. by the `unittest` module (previously it would add a PASS or FAIL
  664. log message).
  665. """
  666. from tornado.options import define, options, parse_command_line
  667. define(
  668. "exception_on_interrupt",
  669. type=bool,
  670. default=True,
  671. help=(
  672. "If true (default), ctrl-c raises a KeyboardInterrupt "
  673. "exception. This prints a stack trace but cannot interrupt "
  674. "certain operations. If false, the process is more reliably "
  675. "killed, but does not print a stack trace."
  676. ),
  677. )
  678. # support the same options as unittest's command-line interface
  679. define("verbose", type=bool)
  680. define("quiet", type=bool)
  681. define("failfast", type=bool)
  682. define("catch", type=bool)
  683. define("buffer", type=bool)
  684. argv = [sys.argv[0]] + parse_command_line(sys.argv)
  685. if not options.exception_on_interrupt:
  686. signal.signal(signal.SIGINT, signal.SIG_DFL)
  687. if options.verbose is not None:
  688. kwargs["verbosity"] = 2
  689. if options.quiet is not None:
  690. kwargs["verbosity"] = 0
  691. if options.failfast is not None:
  692. kwargs["failfast"] = True
  693. if options.catch is not None:
  694. kwargs["catchbreak"] = True
  695. if options.buffer is not None:
  696. kwargs["buffer"] = True
  697. if __name__ == "__main__" and len(argv) == 1:
  698. print("No tests specified", file=sys.stderr)
  699. sys.exit(1)
  700. # In order to be able to run tests by their fully-qualified name
  701. # on the command line without importing all tests here,
  702. # module must be set to None. Python 3.2's unittest.main ignores
  703. # defaultTest if no module is given (it tries to do its own
  704. # test discovery, which is incompatible with auto2to3), so don't
  705. # set module if we're not asking for a specific test.
  706. if len(argv) > 1:
  707. unittest.main(module=None, argv=argv, **kwargs) # type: ignore
  708. else:
  709. unittest.main(defaultTest="all", argv=argv, **kwargs)
  710. if __name__ == "__main__":
  711. main()